Build an Array With Stack Operations Problem
Description
LeetCode Problem 1441.
You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
"Push": pushes an integer to the top of the stack."Pop": removes the integer on the top of the stack.
You also have a stream of the integers in the range [1, n].
Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
- If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
- If the stack is not empty, pop the integer at the top of the stack.
- If, at any moment, the elements in the stack (from the bottom to the top) are equal to
target, do not read new integers from the stream and do not do more operations on the stack.
Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.
Example 1:
Input: target = [1,3], n = 3
Output: ["Push","Push","Pop","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Pop the integer on the top of the stack. s = [1].
Read 3 from the stream and push it to the stack. s = [1,3].
Example 2:
Input: target = [1,2,3], n = 3
Output: ["Push","Push","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Read 3 from the stream and push it to the stack. s = [1,2,3].
Example 3:
Input: target = [1,2], n = 4
Output: ["Push","Push"]
Explanation: Initially the stack s is empty. The last element is the top of the stack.
Read 1 from the stream and push it to the stack. s = [1].
Read 2 from the stream and push it to the stack. s = [1,2].
Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
The answers that read integer 3 from the stream are not accepted.
Constraints:
1 <= target.length <= 1001 <= n <= 1001 <= target[i] <= ntargetis strictly increasing.
Solution
Approach: Simulation
The stream gives 1, 2, 3, ... in order, and target is strictly increasing, so we can walk through target and the stream together with cur, the next number the stream would give:
- While
curis smaller than the next target valuex, the numbercuris not wanted:"Push"it and immediately"Pop"it, then move on. - When
cur == x,"Push"it and keep it.
Stop as soon as the last target value is pushed. That is the tricky rule in Example 3: n can be larger than the last target value, but reading more numbers from the stream is not allowed once the stack equals target. So n itself is never needed in the loop.
Complexity: O(t) time, where t is the last value of target (at most n), and O(t) for the list of operations.
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
ans = []
cur = 1 # the next number the stream will give
for x in target:
while cur < x: # a number missing from target: push it, then pop it right away
ans += ["Push", "Pop"]
cur += 1
ans.append("Push") # x itself stays
cur += 1
# stop after the last target number: n may be larger, but reading more is not allowed
return ans