-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path174_Partition_Equal_Subset_Sum.cpp
54 lines (54 loc) · 1.48 KB
/
174_Partition_Equal_Subset_Sum.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
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool solve(vector<int> &nums, int idx, int sum, vector<vector<int>> &dp)
{
if (idx == 0)
{
if (nums[idx] == sum or sum == 0)
return true;
else
return false;
}
if (dp[idx][sum] != -1)
return dp[idx][sum];
bool notTake = solve(nums, idx - 1, sum, dp);
bool take = false;
if (nums[idx] <= sum)
{
take = solve(nums, idx - 1, sum - nums[idx], dp);
}
return dp[idx][sum] = take or notTake;
}
bool canPartition(vector<int> &arr)
{
int n = arr.size();
if (n == 1)
return false;
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
if (sum % 2 == 1)
return 0;
int k = sum / 2;
vector<vector<bool>> dp(n + 1, vector<bool>(k + 1, 0));
for (int i = 0; i < n; i++)
dp[i][0] = true;
if (arr[0] <= k)
dp[0][arr[0]] = true;
for (int ind = 1; ind < n; ind++)
{
for (int target = 1; target <= k; target++)
{
bool nottake = dp[ind - 1][target];
bool take = false;
if (arr[ind] <= target)
take = dp[ind - 1][target - arr[ind]];
dp[ind][target] = nottake or take;
}
}
return dp[n - 1][k];
}
};