Build an Array With Stack Operations

Medium

Description

Given an array target and n, simulate stack operations (Push, Pop) to build target from stream 1, 2, ..., n. Return the operations.

Examples

Input:target = [1,3], n = 3
Output:["Push","Push","Pop","Push"]
Explanation:

Push 1, push 2, pop, push 3.

Input:target = [2,3,4], n = 4
Output:["Push","Pop","Push","Push","Push"]
Explanation:

Push 1 (not wanting it, so pop it), push 2 (keep it), push 3 (keep it), push 4 (keep it). This demonstrates skipping the first element.

Input:target = [1,5], n = 5
Output:["Push","Push","Pop","Push","Pop","Push","Pop","Push"]
Explanation:

Push 1 (keep it), push 2 (pop it), push 3 (pop it), push 4 (pop it), push 5 (keep it). This shows handling larger gaps in the target array.

Constraints

  • 1 ≤ target.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!