-
Notifications
You must be signed in to change notification settings - Fork 481
/
0473.go
50 lines (42 loc) · 940 Bytes
/
0473.go
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
var t, n, s int
var vis []int
func makesquare(nums []int) bool {
n, s = len(nums), 0
for _, v := range nums {
s += v
}
if n < 4 || s % 4 > 0 {
return false
}
t = s / 4
vis = make([]int, n)
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return dfs(nums, 0, 0, 0)
}
func dfs(nums []int, cur, u, k int) bool {
if u == 4 {
return true
}
if cur == t {
return dfs(nums, 0, u + 1, 0)
}
for i, j := k, 0; i < n; i++ {
if vis[i] == 1 || nums[i] + cur > t {
continue
}
vis[i] = 1
if dfs(nums, nums[i] + cur, u, i + 1) {
return true
}
vis[i] = 0
if cur == 0 || cur + nums[i] == t {
return false
}
j = i
for j < n && nums[i] == nums[j] {
j++
}
i = j - 1
}
return false
}