-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay2.cpp
More file actions
74 lines (62 loc) · 1.88 KB
/
Day2.cpp
File metadata and controls
74 lines (62 loc) · 1.88 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
This problem was asked by Facebook.
Given a multiset of integers, return whether it can be partitioned into two subsets whose sums are the same.
For example, given the multiset {15, 5, 20, 10, 35, 15, 10}, it would return true, since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both add up to 55.
Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't split it up into two subsets that add up to the same sum.
*/
#include <bits/stdc++.h>
using namespace std;
// Top Down Approach With Memoization
/*
bool topDown(vector<int> &arr, int n, int subsetSum, vector<vector<bool>> &memo)
{
if (subsetSum == 0)
{
return true;
}
if (n == 0 or subsetSum < 0)
{
return false;
}
if (memo[n][subsetSum] != NULL)
{
return memo[n][subsetSum];
}
bool res = topDown(arr, n - 1, subsetSum - arr[n - 1], memo) || topDown(arr, n - 1, subsetSum, memo);
memo[n][subsetSum] = res;
return res;
}
*/
// Bottom Up approach Time: O(N * target) Space : O(target)
bool bottomUp(vector<int> &arr, int target)
{
vector<bool> dp(target + 1, false);
dp[0] = true;
for (int num : arr)
{
for (int j = target; j >= num; --j)
{
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
bool canPartition(vector<int> &arr)
{
int sum = accumulate(arr.begin(), arr.end(), 0);
if (sum % 2 != 0)
return false;
int target = sum / 2;
int n = arr.size();
vector<vector<bool>> memo(n + 1, vector<bool>(target + 1, NULL));
// topDown(arr, n - 1, target, memo);
return bottomUp(arr, target);
}
int main()
{
vector<int> arr = {15, 5, 20, 10, 35, 15, 10};
cout << (canPartition(arr) ? "true" : "false") << endl; // true
arr = {15, 5, 20, 10, 35};
cout << (canPartition(arr) ? "true" : "false") << endl; // false
return 0;
}