-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTushars_Birthday_Bombs.cpp
More file actions
48 lines (48 loc) · 1.2 KB
/
Tushars_Birthday_Bombs.cpp
File metadata and controls
48 lines (48 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Greedy Approach <================
// <-------------> Naive One
vector<int> Solution::solve(int A, vector<int> &B) {
int n = B.size();
int idx = 0;
int minstrength = B[idx];
for(int i=1;i<n;i++){
if(minstrength>B[i]){
minstrength = B[i];
idx = i;
}
}
int kicks = A/minstrength;
vector<int> res(kicks,idx);
int sum = minstrength*kicks, c=0,i=0;
while(c<kicks && i<idx){
int tmp = sum - B[res[c]] + B[i];
if(tmp>sum && tmp<=A){
res[c++]= i;
sum = tmp;
}
else
i++;
}
return res;
}
// Dynamic Programming Approach <==============
// ---------------------------- Knapsack Kind of
vector<int> Solution::solve(int A, vector<int> &B) {
int n = B.size();
vector<int> dp(A+1, -1), backtrack(A+1);
backtrack[0] = 0;
for(int i=0;i<=A;i++){
for(int j=0;j<n;j++){
if(i>=B[j] && dp[i]<dp[i-B[j]]+1){
dp[i] = dp[i-B[j]]+1;
backtrack[i] = j;
}
}
}
vector<int> res;
int i = A;
while(i>=0 && (i-B[backtrack[i]])>=0){
res.push_back(backtrack[i]);
i -= B[backtrack[i]];
}
return res;
}