forked from mickey0524/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1046.Last-Stone-Weight.java
More file actions
39 lines (32 loc) · 839 Bytes
/
Copy path1046.Last-Stone-Weight.java
File metadata and controls
39 lines (32 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Comparator;
import java.util.PriorityQueue;
// https://leetcode.com/problems/last-stone-weight/
//
// algorithms
// Easy (64.74%)
// Total Accepted: 4,804
// Total Submissions: 7,420
// beats 100.0% of java submissions
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> q = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int n : stones) {
q.offer(n);
}
while (q.size() > 1) {
int st = q.poll();
int nd = q.poll();
if (st > nd) {
q.offer(st - nd);
}
}
if (q.isEmpty()) {
return 0;
}
return q.peek();
}
}