DMITRII’s Substack

Share this post

13.04.2023

dmitriisamoilenko.substack.com

Discover more from DMITRII’s Substack

I post a problem that I solve every single day
Continue reading
Sign in

13.04.2023

946. Validate Stack Sequences

DMITRII SAMOILENKO
Apr 13, 2023
Share this post

13.04.2023

dmitriisamoilenko.substack.com
Share

[946. Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/description/) medium

fun validateStackSequences(pushed: IntArray, popped: IntArray): Boolean =
with(Stack<Int>()) {
    var pop = 0
    pushed.forEach {
        push(it)
        while (isNotEmpty() && peek() == popped[pop]) {
            pop()
            pop++
        }
    }
    isEmpty()
}

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

[blog post](https://leetcode.com/problems/validate-stack-sequences/solutions/3411131/kotlin-stack/)

#### Telegram

https://t.me/leetcode_daily_unstoppable/179

#### Intuition

Do simulation using a Stack.

#### Approach

  • use one iteration and a second pointer for `pop`

  • empty the stack after inserting an element

#### Complexity

- Time complexity:

O(n)

- Space complexity:

O(n)

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

Share this post

13.04.2023

dmitriisamoilenko.substack.com
Share
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