File tree Expand file tree Collapse file tree 4 files changed +130
-2
lines changed
solution/1000-1099/1046.Last Stone Weight Expand file tree Collapse file tree 4 files changed +130
-2
lines changed Original file line number Diff line number Diff line change 50
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
51
52
52
``` python
53
-
53
+ class Solution :
54
+ def lastStoneWeight (self , stones : List[int ]) -> int :
55
+ h = [- s for s in stones]
56
+ heapify(h)
57
+ while len (h) > 1 :
58
+ y, x = - heappop(h), - heappop(h)
59
+ if x != y:
60
+ heappush(h, x - y)
61
+ return 0 if not h else - h[0 ]
54
62
```
55
63
56
64
### ** Java**
@@ -96,4 +104,42 @@ public:
96
104
};
97
105
```
98
106
107
+ ### **Go**
108
+
109
+ ```go
110
+ func lastStoneWeight(stones []int) int {
111
+ q := &hp{stones}
112
+ heap.Init(q)
113
+ for q.Len() > 1 {
114
+ x, y := q.pop(), q.pop()
115
+ if x != y {
116
+ q.push(x - y)
117
+ }
118
+ }
119
+ if q.Len() > 0 {
120
+ return q.IntSlice[0]
121
+ }
122
+ return 0
123
+ }
124
+
125
+ type hp struct{ sort.IntSlice }
126
+
127
+ func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
128
+ func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
129
+ func (h *hp) Pop() interface{} {
130
+ a := h.IntSlice
131
+ v := a[len(a)-1]
132
+ h.IntSlice = a[:len(a)-1]
133
+ return v
134
+ }
135
+ func (h *hp) push(v int) { heap.Push(h, v) }
136
+ func (h *hp) pop() int { return heap.Pop(h).(int) }
137
+ ```
138
+
139
+ ### ** ...**
140
+
141
+ ```
142
+
143
+ ```
144
+
99
145
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -52,7 +52,15 @@ we combine 1 and 1 to get 0 so the array converts to [1] then that's the val
52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def lastStoneWeight (self , stones : List[int ]) -> int :
57
+ h = [- s for s in stones]
58
+ heapify(h)
59
+ while len (h) > 1 :
60
+ y, x = - heappop(h), - heappop(h)
61
+ if x != y:
62
+ heappush(h, x - y)
63
+ return 0 if not h else - h[0 ]
56
64
```
57
65
58
66
### ** Java**
@@ -96,4 +104,42 @@ public:
96
104
};
97
105
```
98
106
107
+ ### **Go**
108
+
109
+ ```go
110
+ func lastStoneWeight(stones []int) int {
111
+ q := &hp{stones}
112
+ heap.Init(q)
113
+ for q.Len() > 1 {
114
+ x, y := q.pop(), q.pop()
115
+ if x != y {
116
+ q.push(x - y)
117
+ }
118
+ }
119
+ if q.Len() > 0 {
120
+ return q.IntSlice[0]
121
+ }
122
+ return 0
123
+ }
124
+
125
+ type hp struct{ sort.IntSlice }
126
+
127
+ func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
128
+ func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
129
+ func (h *hp) Pop() interface{} {
130
+ a := h.IntSlice
131
+ v := a[len(a)-1]
132
+ h.IntSlice = a[:len(a)-1]
133
+ return v
134
+ }
135
+ func (h *hp) push(v int) { heap.Push(h, v) }
136
+ func (h *hp) pop() int { return heap.Pop(h).(int) }
137
+ ```
138
+
139
+ ### ** ...**
140
+
141
+ ```
142
+
143
+ ```
144
+
99
145
<!-- tabs: end -->
Original file line number Diff line number Diff line change
1
+ func lastStoneWeight (stones []int ) int {
2
+ q := & hp {stones }
3
+ heap .Init (q )
4
+ for q .Len () > 1 {
5
+ x , y := q .pop (), q .pop ()
6
+ if x != y {
7
+ q .push (x - y )
8
+ }
9
+ }
10
+ if q .Len () > 0 {
11
+ return q .IntSlice [0 ]
12
+ }
13
+ return 0
14
+ }
15
+
16
+ type hp struct { sort.IntSlice }
17
+
18
+ func (h hp ) Less (i , j int ) bool { return h .IntSlice [i ] > h .IntSlice [j ] }
19
+ func (h * hp ) Push (v interface {}) { h .IntSlice = append (h .IntSlice , v .(int )) }
20
+ func (h * hp ) Pop () interface {} {
21
+ a := h .IntSlice
22
+ v := a [len (a )- 1 ]
23
+ h .IntSlice = a [:len (a )- 1 ]
24
+ return v
25
+ }
26
+ func (h * hp ) push (v int ) { heap .Push (h , v ) }
27
+ func (h * hp ) pop () int { return heap .Pop (h ).(int ) }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def lastStoneWeight (self , stones : List [int ]) -> int :
3
+ h = [- s for s in stones ]
4
+ heapify (h )
5
+ while len (h ) > 1 :
6
+ y , x = - heappop (h ), - heappop (h )
7
+ if x != y :
8
+ heappush (h , x - y )
9
+ return 0 if not h else - h [0 ]
You can’t perform that action at this time.
0 commit comments