Skip to content

Commit 57ae56f

Browse files
committed
feat: add solutions to lc problem: No.1046
No.1046.Last Stone Weight
1 parent 53ab452 commit 57ae56f

File tree

6 files changed

+119
-53
lines changed

6 files changed

+119
-53
lines changed

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

+46-17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343

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

46+
**方法一:大根堆(优先队列)**
47+
48+
`stones` 数组所有元素放入大根堆,执行循环操作,每次弹出两个元素 `x``y`,如果 `x != y`,将 `x - y` 放入大根堆。当堆元素个数小于 `2` 时,退出循环。
49+
50+
最后如果存在堆顶元素,则将其返回,否则返回 `0`
51+
52+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 是 `stones` 数组的长度。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
@@ -52,13 +60,14 @@
5260
```python
5361
class Solution:
5462
def lastStoneWeight(self, stones: List[int]) -> int:
55-
h = [-s for s in stones]
63+
h = [-v for v in stones]
5664
heapify(h)
5765
while len(h) > 1:
58-
y, x = -heappop(h), -heappop(h)
66+
x = heappop(h)
67+
y = heappop(h)
5968
if x != y:
6069
heappush(h, x - y)
61-
return 0 if not h else -h[0]
70+
return 0 if len(h) == 0 else -h[0]
6271
```
6372

6473
### **Java**
@@ -68,18 +77,18 @@ class Solution:
6877
```java
6978
class Solution {
7079
public int lastStoneWeight(int[] stones) {
71-
Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
72-
for (int stone : stones) {
73-
queue.offer(stone);
80+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
81+
for (int v : stones) {
82+
q.offer(v);
7483
}
75-
while (queue.size() > 1) {
76-
int x = queue.poll();
77-
int y = queue.poll();
84+
while (q.size() > 1) {
85+
int y = q.poll();
86+
int x = q.poll();
7887
if (x != y) {
79-
queue.offer(x - y);
88+
q.offer(y - x);
8089
}
8190
}
82-
return queue.isEmpty() ? 0 : queue.poll();
91+
return q.isEmpty() ? 0 : q.poll();
8392
}
8493
}
8594
```
@@ -92,12 +101,9 @@ public:
92101
int lastStoneWeight(vector<int>& stones) {
93102
priority_queue<int> pq(stones.begin(), stones.end());
94103
while (pq.size() > 1) {
95-
int x = pq.top();
96-
pq.pop();
97-
int y = pq.top();
98-
pq.pop();
99-
if (x != y)
100-
pq.push(x - y);
104+
int x = pq.top(); pq.pop();
105+
int y = pq.top(); pq.pop();
106+
if (x != y) pq.push(x - y);
101107
}
102108
return pq.empty() ? 0 : pq.top();
103109
}
@@ -136,6 +142,29 @@ func (h *hp) push(v int) { heap.Push(h, v) }
136142
func (h *hp) pop() int { return heap.Pop(h).(int) }
137143
```
138144

145+
### **JavaScript**
146+
147+
```js
148+
/**
149+
* @param {number[]} stones
150+
* @return {number}
151+
*/
152+
var lastStoneWeight = function (stones) {
153+
const pq = new MaxPriorityQueue();
154+
for (const v of stones) {
155+
pq.enqueue(v);
156+
}
157+
while (pq.size() > 1) {
158+
const x = pq.dequeue()['priority'];
159+
const y = pq.dequeue()['priority'];
160+
if (x != y) {
161+
pq.enqueue(x - y);
162+
}
163+
}
164+
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
165+
};
166+
```
167+
139168
### **...**
140169

141170
```

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

+38-17
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,33 @@ 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 = [-s for s in stones]
57+
h = [-v for v in stones]
5858
heapify(h)
5959
while len(h) > 1:
60-
y, x = -heappop(h), -heappop(h)
60+
x = heappop(h)
61+
y = heappop(h)
6162
if x != y:
6263
heappush(h, x - y)
63-
return 0 if not h else -h[0]
64+
return 0 if len(h) == 0 else -h[0]
6465
```
6566

6667
### **Java**
6768

6869
```java
6970
class Solution {
7071
public int lastStoneWeight(int[] stones) {
71-
Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
72-
for (int stone : stones) {
73-
queue.offer(stone);
72+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
73+
for (int v : stones) {
74+
q.offer(v);
7475
}
75-
while (queue.size() > 1) {
76-
int x = queue.poll();
77-
int y = queue.poll();
76+
while (q.size() > 1) {
77+
int y = q.poll();
78+
int x = q.poll();
7879
if (x != y) {
79-
queue.offer(x - y);
80+
q.offer(y - x);
8081
}
8182
}
82-
return queue.isEmpty() ? 0 : queue.poll();
83+
return q.isEmpty() ? 0 : q.poll();
8384
}
8485
}
8586
```
@@ -92,12 +93,9 @@ public:
9293
int lastStoneWeight(vector<int>& stones) {
9394
priority_queue<int> pq(stones.begin(), stones.end());
9495
while (pq.size() > 1) {
95-
int x = pq.top();
96-
pq.pop();
97-
int y = pq.top();
98-
pq.pop();
99-
if (x != y)
100-
pq.push(x - y);
96+
int x = pq.top(); pq.pop();
97+
int y = pq.top(); pq.pop();
98+
if (x != y) pq.push(x - y);
10199
}
102100
return pq.empty() ? 0 : pq.top();
103101
}
@@ -136,6 +134,29 @@ func (h *hp) push(v int) { heap.Push(h, v) }
136134
func (h *hp) pop() int { return heap.Pop(h).(int) }
137135
```
138136

137+
### **JavaScript**
138+
139+
```js
140+
/**
141+
* @param {number[]} stones
142+
* @return {number}
143+
*/
144+
var lastStoneWeight = function (stones) {
145+
const pq = new MaxPriorityQueue();
146+
for (const v of stones) {
147+
pq.enqueue(v);
148+
}
149+
while (pq.size() > 1) {
150+
const x = pq.dequeue()['priority'];
151+
const y = pq.dequeue()['priority'];
152+
if (x != y) {
153+
pq.enqueue(x - y);
154+
}
155+
}
156+
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
157+
};
158+
```
159+
139160
### **...**
140161

141162
```

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ class Solution {
33
int lastStoneWeight(vector<int>& stones) {
44
priority_queue<int> pq(stones.begin(), stones.end());
55
while (pq.size() > 1) {
6-
int x = pq.top();
7-
pq.pop();
8-
int y = pq.top();
9-
pq.pop();
10-
if (x != y)
11-
pq.push(x - y);
6+
int x = pq.top(); pq.pop();
7+
int y = pq.top(); pq.pop();
8+
if (x != y) pq.push(x - y);
129
}
1310
return pq.empty() ? 0 : pq.top();
1411
}
15-
};
12+
};
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public int lastStoneWeight(int[] stones) {
3-
Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
4-
for (int stone : stones) {
5-
queue.offer(stone);
3+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
4+
for (int v : stones) {
5+
q.offer(v);
66
}
7-
while (queue.size() > 1) {
8-
int x = queue.poll();
9-
int y = queue.poll();
7+
while (q.size() > 1) {
8+
int y = q.poll();
9+
int x = q.poll();
1010
if (x != y) {
11-
queue.offer(x - y);
11+
q.offer(y - x);
1212
}
1313
}
14-
return queue.isEmpty() ? 0 : queue.poll();
14+
return q.isEmpty() ? 0 : q.poll();
1515
}
16-
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} stones
3+
* @return {number}
4+
*/
5+
var lastStoneWeight = function (stones) {
6+
const pq = new MaxPriorityQueue();
7+
for (const v of stones) {
8+
pq.enqueue(v);
9+
}
10+
while (pq.size() > 1) {
11+
const x = pq.dequeue()['priority'];
12+
const y = pq.dequeue()['priority'];
13+
if (x != y) {
14+
pq.enqueue(x - y);
15+
}
16+
}
17+
return pq.isEmpty() ? 0 : pq.dequeue()['priority'];
18+
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class Solution:
22
def lastStoneWeight(self, stones: List[int]) -> int:
3-
h = [-s for s in stones]
3+
h = [-v for v in stones]
44
heapify(h)
55
while len(h) > 1:
6-
y, x = -heappop(h), -heappop(h)
6+
x = heappop(h)
7+
y = heappop(h)
78
if x != y:
89
heappush(h, x - y)
9-
return 0 if not h else -h[0]
10+
return 0 if len(h) == 0 else -h[0]

0 commit comments

Comments
 (0)