LeetCode Entry
1046. Last Stone Weight
use PriorityQueue with compareByDescending
fun lastStoneWeight(stones: IntArray): Int =
with(PriorityQueue<Int>(compareByDescending { it } )) {
stones.forEach { add(it) }
while (size > 1) add(poll() - poll())
if (isEmpty()) 0 else peek()
}
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/190
Intuition
Just run the simulation.
Approach
- use
PriorityQueuewithcompareByDescendingComplexity
- Time complexity: \(O(nlog(n))\)
- Space complexity: \(O(n)\)