Skip to content

Instantly share code, notes, and snippets.

@humpydonkey
Created April 17, 2021 19:01
Show Gist options
  • Save humpydonkey/597f5adb10e49116f652bd42ad847ba2 to your computer and use it in GitHub Desktop.
Save humpydonkey/597f5adb10e49116f652bd42ad847ba2 to your computer and use it in GitHub Desktop.
class Solution {
// Time: O(nlogn)
// Space: O(n)
public int lastStoneWeight(int[] stones) {
if (stones.length == 1) {
return stones[0];
}
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
for (int weight : stones) {
queue.add(weight);
}
while (queue.size() > 1) {
int y = queue.poll();
int x = queue.poll();
queue.add(y-x);
}
return queue.poll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment