We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1bf1b99 commit a14f47aCopy full SHA for a14f47a
Easy/Last Stone Weight.java
@@ -1,16 +1,16 @@
1
class Solution {
2
public int lastStoneWeight(int[] stones) {
3
- PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
+ PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
4
for (int stone : stones) {
5
pq.add(stone);
6
}
7
while (pq.size() > 1) {
8
- int max1 = pq.poll();
9
- int max2 = pq.poll();
10
- if (max1 != max2) {
11
- pq.add(max1 - max2);
+ int firstStone = pq.poll();
+ int secondStone = pq.poll();
+ if (firstStone != secondStone) {
+ pq.add(firstStone - secondStone);
12
13
14
- return pq.isEmpty() ? 0 : pq.peek();
+ return pq.isEmpty() ? 0 : pq.poll();
15
16
0 commit comments