Open godkingjay opened 10 months ago
class Solution {
public List
int k=1;
int i=0;
while(i<target.length && k<=n){
stack.push(target[i]);
l.add("Push");
if(target[i]==k){
i++;
k++;
}else{
stack.pop();
l.add("Pop");
k++;
}
}
return l;
}
} in JAVA****
Difficulty
Medium
Problem Description
You are given an integer array
target
and an integern
.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: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:
Example 2:
Example 3:
Constraints:
1 <= target.length <= 100
1 <= n <= 100
1 <= target[i] <= n
target
is strictly increasing.Link
https://leetcode.com/problems/build-an-array-with-stack-operations/