LeetCode Entry

1046. Last Stone Weight

24.04.2023 easy 2023 kotlin

use PriorityQueue with compareByDescending

1046. Last Stone Weight easy


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()
}

blog post substack

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/190

Intuition

Just run the simulation.

Approach

  • use PriorityQueue with compareByDescending

    Complexity

  • Time complexity: \(O(nlog(n))\)
  • Space complexity: \(O(n)\)