forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1354.go
More file actions
48 lines (40 loc) · 856 Bytes
/
Copy path1354.go
File metadata and controls
48 lines (40 loc) · 856 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
41
42
43
44
45
46
47
48
type IntHeap []int
func (h IntHeap) Len() int {
return len(h)
}
func (h IntHeap) Less(i, j int) bool {
return h[i] > h[j]
}
func (h IntHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *IntHeap) Pop() interface{} {
x := (*h)[(*h).Len()-1]
*h = (*h)[:(*h).Len()-1]
return x
}
func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.(int))
}
func isPossible(target []int) bool {
var s int64 = 0
h := new(IntHeap)
for _, v := range target {
s += int64(v)
heap.Push(h, int64(v))
}
for true {
pre := heap.Pop(h).(int64)
s -= pre
if pre == 1 || s == 1 {
return true
}
if pre < s || pre % s == 0 {
return false
}
pre %= s
s += pre
heap.Push(h, pre)
}
return false
}