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 Reading material
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
Similar questions