DMITRII’s Substack

Share this post

Leetcode daily # 24.04.2023

dmitriisamoilenko.substack.com

Leetcode daily # 24.04.2023

[1046. Last Stone Weight]

DMITRII SAMOILENKO
Apr 24, 2023
Share
Share this post

Leetcode daily # 24.04.2023

dmitriisamoilenko.substack.com

24.04.2023

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

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)

Thanks for reading DMITRII’s Substack! Subscribe for free to receive new posts and support my work.

Share
Share this post

Leetcode daily # 24.04.2023

dmitriisamoilenko.substack.com
Previous
Next
Comments
Top
New

No posts

Ready for more?

© 2023 DMITRII SAMOILENKO
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing