forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_416.cpp
42 lines (35 loc) · 970 Bytes
/
_416.cpp
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
//416. Partition Equal Subset Sum
class Solution {
public:
//maximum size top-down dp table
bool dp[201][20001];
bool equalPart(vector<int>& arr,int size,int sum){
for(int i=0;i<=size;i++){
dp[i][0]=true;
}
for(int i=1;i<=sum;i++){
dp[0][i]=false;
}
for(int i=1;i<=size;i++){
for(int j=1;j<=sum;j++){
if(arr[i-1]<=j){
dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i-1]];
}else{
dp[i][j] = dp[i-1][j];
}
}
}
return dp[size][sum];
}
bool canPartition(vector<int>& nums) {
int sum=0;
for(int i=0;i<nums.size();i++){
sum+=nums[i];
}
if(sum%2!=0){
return false;
}else{
return equalPart(nums,nums.size(),sum/2);
}
}
};