forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.cpp
57 lines (44 loc) · 1.36 KB
/
main4.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
55
56
57
/// Source : https://leetcode.com/problems/matchsticks-to-square/description/
/// Author : liuyubobobo
/// Time : 2018-08-28
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
/// Dynamic Programming
///
/// Time Complexity: O(n*2^n)
/// Space Complexity: O(2^n)
class Solution {
public:
bool makesquare(vector<int>& nums) {
if(nums.size() == 0)
return false;
int total = accumulate(nums.begin(), nums.end(), 0);
if(total % 4)
return false;
sort(nums.begin(), nums.end(), greater<int>());
int all = (1 << nums.size()) - 1;
int side = total / 4;
vector<int> sum(all + 1, 0);
for(int i = 1; i <= all; i ++)
for(int j = 0; j < nums.size() ; j ++)
if(i & (1 << j)){
sum[i] = nums[j] + sum[i - (1 << j)];
break;
}
// dp[state]: is it possible to make a square from the state.
vector<bool> dp(all + 1, false);
dp[0] = true;
for(int i = 1; i <= all; i ++)
for(int j = 0; j < nums.size(); j ++)
if(i & (1 << j) && dp[i - (1 << j)] && sum[i - (1 << j)] % side + nums[j] <= side){
dp[i] = true;
break;
}
return dp[all];
}
};
int main() {
return 0;
}