24.04.2023
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
PriorityQueue
withcompareByDescending
Complexity
Time complexity:
O(nlog(n))Space complexity:
O(n)