We’re preparing your current view and syncing the latest data.
Given a target array and an integer n, generate the list of stack operations to build the target array using push and pop operations. You start with an empty stack and you sequentially push numbers from 1 to n. To build the target array, you keep pushing numbers and pop them if they are not in the target array. Return the list of operations needed to build the target array in order.
The input consists of a target array of distinct integers target and an integer n representing the range from 1 to n.
Return a list of strings representing the stack operations: "Push" for pushing a number on the stack, "Pop" for popping the top element.
1 <= target.length <= n <= 100; 1 <= target[i] <= n; target is strictly increasing.
Example 1
Input
target = [1,3], n = 3
Output
["Push", "Push", "Pop", "Push"]
Explanation
Push 1 (keep), push 2 (pop since 2 not in target), push 3 (keep)
Example 2
Input
target = [1,2,3], n = 3
Output
["Push", "Push", "Push"]
Explanation
Push 1, 2, and 3 sequentially since all are in the target