-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1046.java
More file actions
42 lines (38 loc) · 1.2 KB
/
prblm1046.java
File metadata and controls
42 lines (38 loc) · 1.2 KB
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
40
41
42
import java.util.*;
public class prblm1046 {
public static void main(String[] args) {
int[] stones = {2,7,4,1,8,1};
System.out.println(lastStoneWeight2(stones));
int[] stones1 = {1};
System.out.println(lastStoneWeight2(stones1));
}
public static int lastStoneWeight(int[] stones) {
List<Integer> list = new ArrayList<>();
for (int num : stones) {
list.add(num);
}
while (list.size() > 1) {
Collections.sort(list);
int x = list.remove(list.size() - 1);
int y = list.remove(list.size() - 1);
if(x != y){
list.add(Math.abs(y - x));
}
}
return list.size() == 0 ? 0 : list.get(0);
}
public static int lastStoneWeight2(int[] stones) {
PriorityQueue<Integer> list = new PriorityQueue<>(Collections.reverseOrder());
for (int num : stones) {
list.add(num);
}
while (list.size() > 1) {
int x = list.poll();
int y = list.poll();
if(x != y){
list.add(x - y);
}
}
return list.isEmpty() ? 0 : list.peek();
}
}