Problem#
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 buildtarget
following the mentioned rules. If there are multiple valid answers, return any of them.
Examples#
Example 1#
1
2
3
4
5
6
7
|
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#
1
2
3
4
5
6
|
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#
1
2
3
4
5
6
7
|
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 <= 100
1 <= n <= 100
1 <= target[i] <= n
target
is strictly increasing.
Solution#
Method 1: Simulate the Stack Operations#
Intuition#
We process the stream from 1 to n, and for each number, if it matches the next number in target
, we push it. If not, we push and immediately pop it. We stop when all elements of target
are built.
Approach#
- Use a pointer to track the current index in
target
.
- For each number from 1 to n:
- If it matches
target[idx]
, append “Push” and move to the next target.
- Otherwise, append “Push” and “Pop”.
- Stop when all elements of
target
are built.
Python#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution:
def buildArray(self, target, n):
res = []
idx = 0
for num in range(1, n+1):
if idx == len(target):
break
if num == target[idx]:
res.append("Push")
idx += 1
else:
res.append("Push")
res.append("Pop")
return res
|
Java#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import java.util.*;
class Solution {
public List<String> buildArray(int[] target, int n) {
List<String> res = new ArrayList<>();
int idx = 0;
for (int num = 1; num <= n && idx < target.length; num++) {
if (num == target[idx]) {
res.add("Push");
idx++;
} else {
res.add("Push");
res.add("Pop");
}
}
return res;
}
}
|
C++#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<string> buildArray(vector<int>& target, int n) {
vector<string> res;
int idx = 0;
for (int num = 1; num <= n && idx < target.size(); ++num) {
if (num == target[idx]) {
res.push_back("Push");
idx++;
} else {
res.push_back("Push");
res.push_back("Pop");
}
}
return res;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
func buildArray(target []int, n int) []string {
res := []string{}
idx := 0
for num := 1; num <= n && idx < len(target); num++ {
if num == target[idx] {
res = append(res, "Push")
idx++
} else {
res = append(res, "Push", "Pop")
}
}
return res
}
|
Kotlin#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Solution {
fun buildArray(target: IntArray, n: Int): List<String> {
val res = mutableListOf<String>()
var idx = 0
for (num in 1..n) {
if (idx == target.size) break
if (num == target[idx]) {
res.add("Push")
idx++
} else {
res.add("Push")
res.add("Pop")
}
}
return res
}
}
|
Rust#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
impl Solution {
pub fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
let mut res = Vec::new();
let mut idx = 0;
for num in 1..=n {
if idx == target.len() { break; }
if num == target[idx] {
res.push("Push".to_string());
idx += 1;
} else {
res.push("Push".to_string());
res.push("Pop".to_string());
}
}
res
}
}
|
Complexity#
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)