Skip to content

Instantly share code, notes, and snippets.

@munguial
Last active April 12, 2020 20:21
Show Gist options
  • Save munguial/6aca47f704165d0507139ce73bbd8797 to your computer and use it in GitHub Desktop.
Save munguial/6aca47f704165d0507139ce73bbd8797 to your computer and use it in GitHub Desktop.
Day 12 - Last Stone Weight
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3297/
import java.util.Collections
import java.util.PriorityQueue
class Solution {
fun lastStoneWeight(stones: IntArray): Int {
val pq = PriorityQueue<Int>(Collections.reverseOrder())
for (s in stones) {
pq.add(s)
}
while (pq.size > 1) {
val res = pq.remove() - pq.remove()
if (res > 0) {
pq.add(res)
}
}
return if (pq.isEmpty()) 0 else pq.remove()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment