-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedium_Prob60.cpp
More file actions
56 lines (45 loc) · 1.33 KB
/
Medium_Prob60.cpp
File metadata and controls
56 lines (45 loc) · 1.33 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
49
50
51
52
53
54
55
56
/* Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same. */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int sumElem(vector<int>* multiset){
int sum = 0;
for (int i=0; i<(*multiset).size(); i++){
sum += (*multiset)[i] ;
}
return sum;
}
// ProgDynResult
bool progDynResult(vector<int>* multiset){
int n = (*multiset).size();
int sum = sumElem(multiset);
if (sum%2 == 1){
return false ;
}
vector<vector<bool>> progDynTab = vector<vector<bool>>(n+1,vector<bool>(sum/2 + 1, false));
for (int i=0; i<= n; i++){
progDynTab[i][0] = true;
}
for (int j=1; j<= sum/2 ; j++){
for (int i=1; i<= n; i++){
if (progDynTab[i-1][j]){
progDynTab[i][j] = true;
} else {
int ibis = j-(*multiset)[i-1] ;
if ((ibis >= 0)){
if (progDynTab[i-1][ibis])
progDynTab[i][j] = true;
}
}
}
}
return progDynTab[n][sum/2];
}
int main(int argc, char *argv[])
{
vector<int> multiset1{15, 5, 20, 10, 35, 15, 10};
vector<int>multiset2{15, 5, 20, 10, 35};
cout << progDynResult(&multiset1) << endl;
cout << progDynResult(&multiset2) << endl;
}