Day 30 — Last Stone Weight

class Solution {
public int lastStoneWeight(int[] stones) {
if(stones == null
|| stones.length == 0) return 0;
PriorityQueue<Integer> pq
= new PriorityQueue<>((a,b)-> b-a);
for(int stone: stones) {
pq.offer(stone);
}
while(!pq.isEmpty()) {
int s1 = pq.poll();
if(!pq.isEmpty()){
int s2 = pq.poll();
if (s1 > s2) pq.offer(s1 - s2);
} else {
return s1;
}
}
return 0;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store