-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
144 lines (109 loc) · 2.35 KB
/
solution.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package laststoneweight
import (
"fmt"
)
func lastStoneWeight(stones []int) int {
// 排序成 max heap
for i := 0; i < len(stones); i++ {
if i == 0 {
continue
}
sort(stones, i)
// fmt.Println("sorted stones:", stones[:i+1])
}
for len(stones) >= 2 {
// 取出最大
x := take(stones)
// fmt.Println("take", x, "from", stones)
stones = stones[:len(stones)-1] // 縮小 slice
// 取出次大
y := take(stones)
// fmt.Println("take", y, "from", stones)
lastId := len(stones) - 1
switch z := x - y; z {
case 0:
stones = stones[:len(stones)-1] // 縮小 slice
default:
stones[lastId] = z
// fmt.Println("put", z, "to", "stones[", lastId, "]")
sort(stones, lastId)
}
// fmt.Println("stones:", stones)
}
if len(stones) == 1 {
return stones[0]
}
return 0
}
func sort(stones []int, index int) {
for {
parentId := (index - 1) / 2
if parentId < 0 || parentId == index {
return
}
parent := stones[parentId]
leaf := stones[index]
if parent >= leaf {
return
}
stones[parentId] = leaf
stones[index] = parent
index = parentId
// fmt.Println("sort:", stones)
}
}
func take(stones []int) int {
// 取出最大值
out := stones[0]
// 將最後一個節點放到 root
leaf := stones[len(stones)-1]
stones[0] = leaf
// stones[len(stones)-1] = out
// 重新排序 (排除最後一個)
length := len(stones) - 1 // 排除最後一個
i := 0
for i < length {
leftId := i*2 + 1
rightId := i*2 + 2
var childId int
switch {
// 左右節點都在
case leftId < length && rightId < length:
// 找出哪個比較大,然後用最大的跟當前節點比較
if stones[leftId] > stones[rightId] {
childId = leftId
} else {
childId = rightId
}
// 只有左節點在
case leftId < length:
childId = leftId
// 只有右節點在
case leftId < length:
childId = rightId
// 左右節點都不在
default:
childId = i
}
// 左右都不在,直接結束
if childId == i {
break
}
child := stones[childId]
current := stones[i]
// 子節點沒有比當前節點大,無需更換
if child <= current {
break
}
stones[childId] = current
stones[i] = child
i = childId
}
return out
}
func main() {
stones := []int{2, 7, 4, 1, 8, 1}
fmt.Println(lastStoneWeight(stones))
stones = []int{1, 3}
fmt.Println(lastStoneWeight(stones))
}