Last Stone Weight LeetCode solution in Java

1
class Solution {
2
public int lastStoneWeight(int[] stones) {
3
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> Integer.compare(y, x));
4
for (int i = 0; i < stones.length; i++) {
5
pq.add(stones[i]);
6
}
7
while (pq.size() > 1) {
8
int r1 = pq.poll();
9
int r2 = pq.poll();
10
if (r1 != r2) pq.add(r1 - r2);
11
}
12
return (pq.isEmpty()) ? 0 : pq.poll();
13
}
14
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0