Skip to content

Commit 9efd871

Browse files
committed
feat: add solutions to lc problem: No.1046
No.1046.Last Stone Weight
1 parent b5f680c commit 9efd871

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

solution/1000-1099/1046.Last Stone Weight/README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```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]
5462
```
5563

5664
### **Java**
@@ -96,4 +104,42 @@ public:
96104
};
97105
```
98106
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+
99145
<!-- tabs:end -->

solution/1000-1099/1046.Last Stone Weight/README_EN.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ we combine 1 and 1 to get 0 so the array converts to [1] then that&#39;s the val
5252
### **Python3**
5353

5454
```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]
5664
```
5765

5866
### **Java**
@@ -96,4 +104,42 @@ public:
96104
};
97105
```
98106
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+
99145
<!-- tabs:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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) }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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]

0 commit comments

Comments
 (0)