Skip to content

Practice Build an Array With Stack Operations

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1441
LeetCode Problem

Build an Array With Stack Operations

Problem Statement

Start with an empty stack and read the stream 1, 2, ..., n in order. A "Push" operation reads the next stream integer and puts it on top of the stack; a "Pop" removes the current top and is allowed only when the stack is nonempty. Build target as the stack contents from bottom to top. Stop immediately when they match: do not read further integers or perform more operations. Return any valid list of operation names.

Source: Build an Array With Stack Operations on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

Input
target = [1,2,3], n = 3
Output
["Push","Push","Push"]

# Example 3

Input
target = [1,2], n = 4
Output
["Push","Push"]

Constraints

1 <= target.length <= 100
1 <= n <= 100
1 <= target[i] <= n
target is strictly increasing.

Related LeetCode Problems

Practice more problems similar to Build an Array With Stack Operations.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.