1441. Build an Array With Stack Operations
T: O(n)
S: O(1)
```java
class Solution {
public List<String> buildArray(int[] target, int n) {
int tIndex = 0;
int i = 1;
List<String> result = new ArrayList<>();
while (tIndex < target.length && i <= n) {
if (target[tIndex] == i) {
result.add("Push");
tIndex++;
} else {
result.add("Push");
result.add("Pop");
}
i++;
}
return result;
}
}
/***
T: O(n)
S: O(1)
in short, when target value not in [1...n] -> add result with "Push" and "Pop"
others all add "Push"
3
1
pushpop pushpop
1 2 3 4.5 6 7
1 2 5 7
*/
```
Last updated