Skip to content

Commit 7194bce

Browse files
committed
feat: add solutions to lc problem: No.1046
No.1046.Last Stone Weight
1 parent 0dc873c commit 7194bce

File tree

8 files changed

+120
-52
lines changed

8 files changed

+120
-52
lines changed

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
**方法一:大根堆(优先队列)**
46+
**方法一:优先队列(大根堆**
4747

48-
`stones` 数组所有元素放入大根堆,执行循环操作,每次弹出两个元素 `x``y`,如果 `x != y`,将 `x - y` 放入大根堆。当堆元素个数小于 `2` 时,退出循环。
48+
我们将数组 `stones` 所有元素放入大根堆,然后执行循环操作,每次弹出两个元素 $y$$x$,如果 $x \neq y$,将 $y - x$ 放入大根堆。当堆元素个数小于 $2$ 时,退出循环。
4949

50-
最后如果存在堆顶元素,则将其返回,否则返回 `0`
50+
最后如果存在堆顶元素,则将其返回,否则返回 $0$
5151

52-
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ `stones` 数组的长度
52+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `stones` 的长度
5353

5454
<!-- tabs:start -->
5555

@@ -60,14 +60,13 @@
6060
```python
6161
class Solution:
6262
def lastStoneWeight(self, stones: List[int]) -> int:
63-
h = [-v for v in stones]
63+
h = [-x for x in stones]
6464
heapify(h)
6565
while len(h) > 1:
66-
x = heappop(h)
67-
y = heappop(h)
66+
y, x = -heappop(h), -heappop(h)
6867
if x != y:
6968
heappush(h, x - y)
70-
return 0 if len(h) == 0 else -h[0]
69+
return 0 if not h else -h[0]
7170
```
7271

7372
### **Java**
@@ -78,8 +77,8 @@ class Solution:
7877
class Solution {
7978
public int lastStoneWeight(int[] stones) {
8079
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
81-
for (int v : stones) {
82-
q.offer(v);
80+
for (int x : stones) {
81+
q.offer(x);
8382
}
8483
while (q.size() > 1) {
8584
int y = q.poll();
@@ -99,11 +98,18 @@ class Solution {
9998
class Solution {
10099
public:
101100
int lastStoneWeight(vector<int>& stones) {
102-
priority_queue<int> pq(stones.begin(), stones.end());
101+
priority_queue<int> pq;
102+
for (int x : stones) {
103+
pq.push(x);
104+
}
103105
while (pq.size() > 1) {
104-
int x = pq.top(); pq.pop();
105-
int y = pq.top(); pq.pop();
106-
if (x != y) pq.push(x - y);
106+
int y = pq.top();
107+
pq.pop();
108+
int x = pq.top();
109+
pq.pop();
110+
if (x != y) {
111+
pq.push(y - x);
112+
}
107113
}
108114
return pq.empty() ? 0 : pq.top();
109115
}
@@ -117,9 +123,9 @@ func lastStoneWeight(stones []int) int {
117123
q := &hp{stones}
118124
heap.Init(q)
119125
for q.Len() > 1 {
120-
x, y := q.pop(), q.pop()
126+
y, x := q.pop(), q.pop()
121127
if x != y {
122-
q.push(x - y)
128+
q.push(y - x)
123129
}
124130
}
125131
if q.Len() > 0 {
@@ -151,20 +157,39 @@ func (h *hp) pop() int { return heap.Pop(h).(int) }
151157
*/
152158
var lastStoneWeight = function (stones) {
153159
const pq = new MaxPriorityQueue();
154-
for (const v of stones) {
155-
pq.enqueue(v);
160+
for (const x of stones) {
161+
pq.enqueue(x);
156162
}
157163
while (pq.size() > 1) {
158-
const x = pq.dequeue()['priority'];
159164
const y = pq.dequeue()['priority'];
165+
const x = pq.dequeue()['priority'];
160166
if (x != y) {
161-
pq.enqueue(x - y);
167+
pq.enqueue(y - x);
162168
}
163169
}
164170
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
165171
};
166172
```
167173

174+
### **TypeScript**
175+
176+
```ts
177+
function lastStoneWeight(stones: number[]): number {
178+
const pq = new MaxPriorityQueue();
179+
for (const x of stones) {
180+
pq.enqueue(x);
181+
}
182+
while (pq.size() > 1) {
183+
const y = pq.dequeue().element;
184+
const x = pq.dequeue().element;
185+
if (x !== y) {
186+
pq.enqueue(y - x);
187+
}
188+
}
189+
return pq.isEmpty() ? 0 : pq.dequeue().element;
190+
}
191+
```
192+
168193
### **...**
169194

170195
```

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

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@ we combine 1 and 1 to get 0 so the array converts to [1] then that&#39;s the val
5454
```python
5555
class Solution:
5656
def lastStoneWeight(self, stones: List[int]) -> int:
57-
h = [-v for v in stones]
57+
h = [-x for x in stones]
5858
heapify(h)
5959
while len(h) > 1:
60-
x = heappop(h)
61-
y = heappop(h)
60+
y, x = -heappop(h), -heappop(h)
6261
if x != y:
6362
heappush(h, x - y)
64-
return 0 if len(h) == 0 else -h[0]
63+
return 0 if not h else -h[0]
6564
```
6665

6766
### **Java**
@@ -70,8 +69,8 @@ class Solution:
7069
class Solution {
7170
public int lastStoneWeight(int[] stones) {
7271
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
73-
for (int v : stones) {
74-
q.offer(v);
72+
for (int x : stones) {
73+
q.offer(x);
7574
}
7675
while (q.size() > 1) {
7776
int y = q.poll();
@@ -91,11 +90,18 @@ class Solution {
9190
class Solution {
9291
public:
9392
int lastStoneWeight(vector<int>& stones) {
94-
priority_queue<int> pq(stones.begin(), stones.end());
93+
priority_queue<int> pq;
94+
for (int x : stones) {
95+
pq.push(x);
96+
}
9597
while (pq.size() > 1) {
96-
int x = pq.top(); pq.pop();
97-
int y = pq.top(); pq.pop();
98-
if (x != y) pq.push(x - y);
98+
int y = pq.top();
99+
pq.pop();
100+
int x = pq.top();
101+
pq.pop();
102+
if (x != y) {
103+
pq.push(y - x);
104+
}
99105
}
100106
return pq.empty() ? 0 : pq.top();
101107
}
@@ -109,9 +115,9 @@ func lastStoneWeight(stones []int) int {
109115
q := &hp{stones}
110116
heap.Init(q)
111117
for q.Len() > 1 {
112-
x, y := q.pop(), q.pop()
118+
y, x := q.pop(), q.pop()
113119
if x != y {
114-
q.push(x - y)
120+
q.push(y - x)
115121
}
116122
}
117123
if q.Len() > 0 {
@@ -143,20 +149,39 @@ func (h *hp) pop() int { return heap.Pop(h).(int) }
143149
*/
144150
var lastStoneWeight = function (stones) {
145151
const pq = new MaxPriorityQueue();
146-
for (const v of stones) {
147-
pq.enqueue(v);
152+
for (const x of stones) {
153+
pq.enqueue(x);
148154
}
149155
while (pq.size() > 1) {
150-
const x = pq.dequeue()['priority'];
151156
const y = pq.dequeue()['priority'];
157+
const x = pq.dequeue()['priority'];
152158
if (x != y) {
153-
pq.enqueue(x - y);
159+
pq.enqueue(y - x);
154160
}
155161
}
156162
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
157163
};
158164
```
159165

166+
### **TypeScript**
167+
168+
```ts
169+
function lastStoneWeight(stones: number[]): number {
170+
const pq = new MaxPriorityQueue();
171+
for (const x of stones) {
172+
pq.enqueue(x);
173+
}
174+
while (pq.size() > 1) {
175+
const y = pq.dequeue().element;
176+
const x = pq.dequeue().element;
177+
if (x !== y) {
178+
pq.enqueue(y - x);
179+
}
180+
}
181+
return pq.isEmpty() ? 0 : pq.dequeue().element;
182+
}
183+
```
184+
160185
### **...**
161186

162187
```

solution/1000-1099/1046.Last Stone Weight/Solution.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
class Solution {
22
public:
33
int lastStoneWeight(vector<int>& stones) {
4-
priority_queue<int> pq(stones.begin(), stones.end());
4+
priority_queue<int> pq;
5+
for (int x : stones) {
6+
pq.push(x);
7+
}
58
while (pq.size() > 1) {
6-
int x = pq.top();
7-
pq.pop();
89
int y = pq.top();
910
pq.pop();
10-
if (x != y) pq.push(x - y);
11+
int x = pq.top();
12+
pq.pop();
13+
if (x != y) {
14+
pq.push(y - x);
15+
}
1116
}
1217
return pq.empty() ? 0 : pq.top();
1318
}

solution/1000-1099/1046.Last Stone Weight/Solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ func lastStoneWeight(stones []int) int {
22
q := &hp{stones}
33
heap.Init(q)
44
for q.Len() > 1 {
5-
x, y := q.pop(), q.pop()
5+
y, x := q.pop(), q.pop()
66
if x != y {
7-
q.push(x - y)
7+
q.push(y - x)
88
}
99
}
1010
if q.Len() > 0 {

solution/1000-1099/1046.Last Stone Weight/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public int lastStoneWeight(int[] stones) {
33
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
4-
for (int v : stones) {
5-
q.offer(v);
4+
for (int x : stones) {
5+
q.offer(x);
66
}
77
while (q.size() > 1) {
88
int y = q.poll();

solution/1000-1099/1046.Last Stone Weight/Solution.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
*/
55
var lastStoneWeight = function (stones) {
66
const pq = new MaxPriorityQueue();
7-
for (const v of stones) {
8-
pq.enqueue(v);
7+
for (const x of stones) {
8+
pq.enqueue(x);
99
}
1010
while (pq.size() > 1) {
11-
const x = pq.dequeue()['priority'];
1211
const y = pq.dequeue()['priority'];
12+
const x = pq.dequeue()['priority'];
1313
if (x != y) {
14-
pq.enqueue(x - y);
14+
pq.enqueue(y - x);
1515
}
1616
}
1717
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def lastStoneWeight(self, stones: List[int]) -> int:
3-
h = [-v for v in stones]
3+
h = [-x for x in stones]
44
heapify(h)
55
while len(h) > 1:
6-
x = heappop(h)
7-
y = heappop(h)
6+
y, x = -heappop(h), -heappop(h)
87
if x != y:
98
heappush(h, x - y)
10-
return 0 if len(h) == 0 else -h[0]
9+
return 0 if not h else -h[0]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function lastStoneWeight(stones: number[]): number {
2+
const pq = new MaxPriorityQueue();
3+
for (const x of stones) {
4+
pq.enqueue(x);
5+
}
6+
while (pq.size() > 1) {
7+
const y = pq.dequeue().element;
8+
const x = pq.dequeue().element;
9+
if (x !== y) {
10+
pq.enqueue(y - x);
11+
}
12+
}
13+
return pq.isEmpty() ? 0 : pq.dequeue().element;
14+
}

0 commit comments

Comments
 (0)