Skip to content

Commit f45839e

Browse files
committed
feat:1046. Last Stone Weight
1 parent 48e887e commit f45839e

File tree

3 files changed

+141
-7
lines changed

3 files changed

+141
-7
lines changed

Leetcode/1046.Last-Stone-Weight/README.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: "1046.Last-Stone-Weight"
1010
license: ""
1111
images: []
1212

13-
tags: [LeetCode, Go, Easy, Heap, Last Stone Weight]
13+
tags: [LeetCode, Go, Easy, Heap, Priority Queue, Last Stone Weight]
1414
categories: [LeetCode]
1515

1616
featuredImage: ""
@@ -120,9 +120,13 @@ Explanation:
120120
5. 如果最大堆中有石頭,返回該石頭的重量,否則返回 0。
121121
這樣的做法確保每次都選擇最大的兩個石頭進行粉碎,最終留下的石頭重量就是可能的最後一個石頭的重量。
122122

123+
參考 [0215 Kth Largest Element in an Array](../0215.Kth-Largest-Element-in-an-Array/README.md)
124+
123125
## Big O
124-
時間複雜 : ``
125-
空間複雜 : ``
126+
時間複雜 : `O(nlogn)`
127+
空間複雜 : `O(n)`
128+
129+
`n` 是石頭數量. 每次從隊列中取出元素需要話`O(logn)` 的時間, 最多共需要需要粉碎 `n−1` 次石頭
126130

127131
## 來源
128132
* https://leetcode.com/problems/last-stone-weight/
@@ -132,6 +136,73 @@ Explanation:
132136
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/1046.Last-Stone-Weight/main.go
133137

134138
```go
139+
package laststoneweight
140+
141+
import (
142+
"container/heap"
143+
"fmt"
144+
"sort"
145+
)
146+
147+
/*
148+
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
149+
type IntSlice []int
150+
151+
func (x IntSlice) Len() int { return len(x) }
152+
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
153+
func (x IntSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
154+
155+
// Sort is a convenience method: x.Sort() calls Sort(x).
156+
func (x IntSlice) Sort() { Sort(x) }
157+
*/
158+
159+
type hp struct {
160+
sort.IntSlice
161+
}
162+
163+
func (h hp) Less(i, j int) bool {
164+
// 大到小排序
165+
return h.IntSlice[i] > h.IntSlice[j]
166+
}
167+
168+
func (h *hp) Push(v interface{}) {
169+
h.IntSlice = append(h.IntSlice, v.(int))
170+
}
171+
172+
func (h *hp) Pop() interface{} {
173+
old := h.IntSlice
174+
v := old[len(old)-1]
175+
h.IntSlice = old[:len(old)-1]
176+
return v
177+
}
178+
179+
func (h *hp) PushInt(v int) {
180+
heap.Push(h, v)
181+
}
182+
183+
func (h *hp) PopInt() int {
184+
return heap.Pop(h).(int)
185+
}
186+
187+
// 時間複雜 O(nlogn), 空間複雜 O(n)
188+
// n 是石頭數量. 每次從隊列中取出元素需要話O(logn) 的時間, 最多共需要需要粉碎 n−1 次石頭
189+
func LastStoneWeight(stones []int) int {
190+
q := &hp{stones}
191+
heap.Init(q)
192+
fmt.Println(q)
193+
for q.Len() > 1 {
194+
fmt.Println(q)
195+
x, y := q.PopInt(), q.PopInt()
196+
fmt.Printf("%d,%d\n", x, y)
197+
if x > y {
198+
q.PushInt(x - y)
199+
}
200+
}
201+
if q.Len() > 0 {
202+
return q.IntSlice[0]
203+
}
204+
return 0
205+
}
135206

136207
```
137208

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
11
package laststoneweight
22

3-
// 時間複雜 O(), 空間複雜 O()
4-
func LastStoneWeight(stones []int) int {
3+
import (
4+
"container/heap"
5+
"fmt"
6+
"sort"
7+
)
8+
9+
/*
10+
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
11+
type IntSlice []int
12+
13+
func (x IntSlice) Len() int { return len(x) }
14+
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
15+
func (x IntSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
516
17+
// Sort is a convenience method: x.Sort() calls Sort(x).
18+
func (x IntSlice) Sort() { Sort(x) }
19+
*/
20+
21+
type hp struct {
22+
sort.IntSlice
23+
}
24+
25+
func (h hp) Less(i, j int) bool {
26+
// 大到小排序
27+
return h.IntSlice[i] > h.IntSlice[j]
28+
}
29+
30+
func (h *hp) Push(v interface{}) {
31+
h.IntSlice = append(h.IntSlice, v.(int))
32+
}
33+
34+
func (h *hp) Pop() interface{} {
35+
old := h.IntSlice
36+
v := old[len(old)-1]
37+
h.IntSlice = old[:len(old)-1]
38+
return v
39+
}
40+
41+
func (h *hp) PushInt(v int) {
42+
heap.Push(h, v)
43+
}
44+
45+
func (h *hp) PopInt() int {
46+
return heap.Pop(h).(int)
47+
}
48+
49+
// 時間複雜 O(nlogn), 空間複雜 O(n)
50+
// n 是石頭數量. 每次從隊列中取出元素需要話O(logn) 的時間, 最多共需要需要粉碎 n−1 次石頭
51+
func LastStoneWeight(stones []int) int {
52+
q := &hp{stones}
53+
heap.Init(q)
54+
fmt.Println(q)
55+
for q.Len() > 1 {
56+
fmt.Println(q)
57+
x, y := q.PopInt(), q.PopInt()
58+
fmt.Printf("%d,%d\n", x, y)
59+
if x > y {
60+
q.PushInt(x - y)
61+
}
62+
}
63+
if q.Len() > 0 {
64+
return q.IntSlice[0]
65+
}
66+
return 0
667
}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ https://kimi0230.github.io/LeetcodeGolang/
1515
- [Linked List](#linked-list)
1616
- [HashSet \& HashMap](#hashset--hashmap)
1717
- [Stack \& Queue](#stack--queue)
18-
- [Heap](#heap)
18+
- [Heap \& Priority Queue](#heap--priority-queue)
1919
- [Disjoint Set Union](#disjoint-set-union)
2020
- [Trie](#trie)
2121
- [Binary Indexed Tree](#binary-indexed-tree)
@@ -100,10 +100,12 @@ https://kimi0230.github.io/LeetcodeGolang/
100100
| [0094](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0094.Binary-Tree-Inorder-Traversal/) | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0094.Binary-Tree-Inorder-Traversal) | Medium | O(n) | O(1) | Stack |
101101

102102

103-
#### Heap
103+
#### Heap & Priority Queue
104104
| No. | Title | Solution | Difficulty | Time | Space | Topic |
105105
|--------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------:|------------|------------------|-------|----------------------|
106106
| [0703](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0703.Kth-Largest-Element-in-a-Stream/) | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0703.Kth-Largest-Element-in-a-Stream) | Easy | O(K + (N-K)logK) | O(k) | Heap, Priority Queue |
107+
| [1046](https://kimi0230.github.io/LeetcodeGolang/Leetcode/1046.Last-Stone-Weight/) | [Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/1046.Last-Stone-Weight) | Easy | O(nlogn) | O(n) | Heap, Priority Queue |
108+
107109

108110
#### Disjoint Set Union
109111

0 commit comments

Comments
 (0)