diff --git a/Leetcode/Last Stone Weight.java b/Leetcode/Last Stone Weight.java new file mode 100644 index 00000000..9e3ff570 --- /dev/null +++ b/Leetcode/Last Stone Weight.java @@ -0,0 +1,33 @@ +class Solution { + public int lastStoneWeight(int[] stones) { + + PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); + + for(int i : stones){ + pq.add(i); + } + + Iterator it = pq.iterator(); + + while(it.hasNext()){ + + if(pq.size() == 1){ + return pq.peek(); + } + + int a = pq.poll(); + int b = pq.poll(); + + + + if(a>b){ + a = a-b; + pq.add(a); + }else{ + b = b-a; + pq.add(b); + } + } + return 0; + } +}