Created
April 17, 2021 19:01
-
-
Save humpydonkey/597f5adb10e49116f652bd42ad847ba2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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