-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (37 loc) · 931 Bytes
/
main.cpp
File metadata and controls
40 lines (37 loc) · 931 Bytes
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
// 01背包,数组中的元素能否填满sum/2的背包。
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if(sum % 2) return false;
int target = sum / 2;
if(*max_element(nums.begin(), nums.end()) > target) return false;
vector<bool> dp(target + 1, false);
dp[0] = 1;
for(auto num : nums)
{
for(int i = target; i >= 1; --i)
{
if(dp[i - num])
{
dp[i] = true;
if(dp[target] == true)
return true;
}
}
}
return false;
}
};
int main()
{
Solution s;
vector<int> nums = {1,5,11,5};
cout << s.canPartition(nums) << endl;
return 0;
}