forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0473.js
31 lines (25 loc) · 839 Bytes
/
0473.js
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
var makesquare = function(nums) {
let n = nums.length, s = 0;
for (let i of nums) s += i;
if (n < 4 || s % 4) return false;
t = Math.floor(s / 4);
let vis = new Array(n);
vis.fill(0);
nums.sort((a, b) => b - a);
let dfs = function(cur, u, k) {
if (u == 4) return true;
if (cur == t) return dfs(0, u + 1, 0);
for (let i = k, j; i < n; i++) {
if (vis[i] || nums[i] + cur > t) continue;
vis[i] = 1;
if (dfs(cur + nums[i], u, i + 1)) return true;
vis[i] = 0;
if (cur == 0 || nums[i] + cur == t) return false;
j = i;
while (j < n && nums[i] == nums[j]) ++j;
i = j - 1;
}
return false;
}
return dfs(0, 0, 0);
};