Skip to content

Commit f84838e

Browse files
committed
bestSum - tabulation & vector of pointers (C++)
1 parent d52b6df commit f84838e

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// given a target an an array of non-negative integers, return the smallest combination of values that sum up to the target value.
2+
// the numbers can be used repeatedly. If no combination possible return NULL
3+
4+
#include<iostream>
5+
#include<vector>
6+
using namespace std;
7+
8+
vector<int>* createVector() {
9+
vector<int> *new_v = new vector<int>();
10+
return new_v;
11+
}
12+
13+
// create a new vector and copies the values from the vector passed as parameter
14+
vector<int>* createVector(vector<int> prev){
15+
vector<int> *new_v = new vector<int>();
16+
17+
for(int x: prev){
18+
new_v->push_back(x);
19+
}
20+
return new_v;
21+
}
22+
23+
// compare two vectors to see which is the shorter one
24+
void compareAndProceed(vector<int> *potentialRes, vector<int> *originalRes, int ele){
25+
return;
26+
}
27+
28+
vector<int>* bestSum_tab(int target, vector<int> arr) {
29+
// init a vector of vectors where each subvector is initially nullptr
30+
vector<vector<int>*> table(target+1, nullptr);
31+
32+
table[0] = createVector();
33+
34+
for(int i=0; i<table.size(); i++){
35+
// continue only for valid/true positions
36+
if(table[i] != nullptr){
37+
// iterate over all possible shifts (i.e., arr values)
38+
for(int j=0; j<arr.size(); j++) {
39+
40+
// check for out of bounds
41+
if(i + arr[j] < table.size()){
42+
// compare the length between the result at new position (if any) and the potential result in this iteration
43+
// null -> no comparison simply copy, else compare size and copy if potential result is shorter
44+
if(table[i + arr[j]] == nullptr
45+
|| table[i + arr[j]]->size() > table[i]->size())
46+
{
47+
table[i + arr[j]] = createVector(*table[i]);
48+
table[i + arr[j]]->push_back(arr[j]);
49+
}
50+
}
51+
}
52+
}
53+
}
54+
return table[target];
55+
}
56+
57+
58+
int main() {
59+
60+
int n, iter = 1, temp, target;
61+
cout<<"Enter number of elements in array: ";
62+
cin>>n;
63+
64+
vector<int> arr;
65+
while(iter <= n) {
66+
cout<<"Enter element "<<iter<<": ";
67+
cin>>temp;
68+
arr.push_back(temp);
69+
++iter;
70+
}
71+
72+
cout<<"Enter target: ";
73+
cin>>target;
74+
75+
vector<int> *bestRes = bestSum_tab(target, arr);
76+
77+
if(bestRes == nullptr)
78+
cout<<target<<" cannot be generated from array"<<endl;
79+
else {
80+
cout<<target<<" can be generated from array elements: ";
81+
for(int num : *bestRes){
82+
cout<<num<<" ";
83+
}
84+
cout<<endl;
85+
}
86+
87+
return 0;
88+
}

Dynamic Programming/C++/howSum_tab.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ vector<int>* createVector(vector<int> prev){
2727
}
2828

2929
vector<int>* howSum_tab(int target, vector<int> arr) {
30-
// init a vector of vectors where each subvector has -1
30+
// init a vector of vectors where each subvector is initially nullptr
3131
vector<vector<int>*> table(target+1, nullptr);
3232

3333
// set the first element of the table to an empty vector
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# given a target an an array of non-negative integers, return the smallest combination of values that sum up to the target value.
2+
# the numbers can be used repeatedly. If no combination possible return NULL
3+
4+
def bestSum_tab(target, arr):
5+
6+
table = [None for _ in range(target+1)]
7+
8+
# possible to generate 0 by not using any number from arr
9+
table[0] = []
10+
11+
for i in range(len(table)):
12+
# check for valid positions
13+
if not(table[i] == None):
14+
for j in range(len(arr)):
15+
# check for out of bounds
16+
if(i + arr[j] < len(table)):
17+
# if new position is None or if new position has a longer result - copy the original (table[i])
18+
if(table[i + arr[j]] == None or len(table[i + arr[j]]) > len(table[i])):
19+
table[i + arr[j]] = table[i][:]
20+
table[i + arr[j]].append(arr[j])
21+
22+
return table[target]
23+
24+
if __name__ == "__main__":
25+
n = int(input("Enter number of elements: "))
26+
i = 0
27+
arr = []
28+
while(i<n):
29+
t = int(input(f"Enter element {i}: "))
30+
arr.append(t)
31+
i += 1
32+
33+
target = int(input("Enter target: "))
34+
35+
res = bestSum_tab(target, arr)
36+
if(res):
37+
print(f"{target} can be generated from array elements {res}")
38+
else:
39+
print(f"{target} cannot be generated from array")

0 commit comments

Comments
 (0)