Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 623 Bytes

1441.md

File metadata and controls

25 lines (23 loc) · 623 Bytes

1441. Build an Array With Stack Operations

Solution 1 (time O(n), space O(1))

class Solution(object):
    def buildArray(self, target, n):
        """
        :type target: List[int]
        :type n: int
        :rtype: List[str]
        """
        ans = []
        p = 0
        for cur_num in range(1, n + 1):
            if target[p] == cur_num:
                ans.append("Push")
                p += 1
                if p >= len(target):
                    break
            else:
                ans.append("Push")
                ans.append("Pop")
        return ans