Skip to content
LC-1441 Medium LeetCode

1441. Build an Array With Stack Operations

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 80% Topics: Array, Stack, Simulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def buildArray(self, target, n):
        """
        :type target: List[int]
        :type n: int
        :rtype: List[str]
        """
        result, curr = [], 1
        for t in target:
            result.extend(["Push", "Pop"]*(t-curr))
            result.append("Push")
            curr = t+1
        return result

Solution from kamyu104/LeetCode-Solutions · MIT