-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
33 lines (31 loc) · 808 Bytes
/
Copy pathsolution.java
File metadata and controls
33 lines (31 loc) · 808 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
// 1046. Last Stone Weight
// https://leetcode.com/problems/last-stone-weight/
// Easy | Java | Accepted 2025-11-29
// Runtime 1 ms | Memory 43 MB
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> max = new PriorityQueue<>(Collections.reverseOrder());
for(int i = 0; i<stones.length; i++)
{
max.add(stones[i]);
}
while(max.size()>1)
{
int stone1 = max.remove();
int stone2 = max.remove();
if(stone1==stone2)
{
continue;
}
if(stone1!=stone2)
{
max.add(Math.abs(stone1-stone2));
}
}
if(max.size()==0)
{
return 0;
}
return max.peek();
}
}