Skip to content

Commit

Permalink
Create 1046-Last-Stone-Weight.java
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Jul 6, 2022
1 parent 1ad2194 commit bc1b725
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions java/1046-Last-Stone-Weight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> maxHeap = new PriorityQueue();
for (int stone: stones)
maxHeap.add(-stone);
while (maxHeap.size()>1) {
int stone1 = maxHeap.remove();
int stone2 = maxHeap.remove();
if (stone1!=stone2)
maxHeap.add(stone1-stone2);
}
return maxHeap.size()!=0?(-maxHeap.remove()):0;
}
}

0 comments on commit bc1b725

Please sign in to comment.