Build a Target Stack from an Increasing Stream. Start with an empty stack and the stream1,2,...,n. A "Push" consumes the next stream integer and places it on the stack; a "Pop" removes the nonempty stack's top.
### Problem
Start with an empty stack and the stream 1,2,...,n. A "Push" consumes the next stream integer and places it on the stack; a "Pop" removes the nonempty stack's top. Produce an operation list that makes the stack, bottom to top, equal target. Stop immediately when target is reached: read no later integer and perform no later operation. Return any valid operation list.
```cpp
class Solution {
public:
vector<string> buildArray(vector<int>& target, int n);
};
```
### Examples
**Example 1:**
```text
Input: target = [2], n = 3
Output: ["Push","Pop","Push"]
Explanation: Discard1,keep2,and stop before consuming3.
```
**Example 2:**
```text
Input: target = [3], n = 3
Output: ["Push","Push","Pop","Pop","Push"]
Explanation: Push1 and2,pop2 then1,and push3 to reach the target. Immediate push/pop pairs for1 and2 would also be valid.
```
### Constraints
- target has length 1 to 100.
- `1 <= n <= 100` and `1 <= target[i] <= n`.
- target is strictly increasing.
### Contract considerations
The native result remains `vector<string>`; validity of the returned operation sequence is the substantive output contract.
```hint Read the boundary condition
Check stream consumption, nonempty-stack pops, and immediate stopping when the target is first reached. Different legal operation lists remain acceptable.
```
### What a Strong Answer Covers
The native result remains `vector<string>`; validity of the returned operation sequence is the substantive output contract.
### Follow-up Questions
Optional practice extension,separate from the original task: How would you validate an arbitrary returned operation list while enforcing immediate stopping?
Overview: Build a Target Stack from an Increasing Stream. Start with an empty stack and the stream1,2,...,n. A "Push" consumes the next stream integer and places it on the stack; a "Pop" removes the nonempty stack's top.
Start with an empty stack and the stream 1,2,...,n. A "Push" consumes the next stream integer and places it on the stack; a "Pop" removes the nonempty stack's top. Produce an operation list that makes the stack, bottom to top, equal target. Stop immediately when target is reached: read no later integer and perform no later operation. Return any valid operation list.
class Solution {
public:
vector<string> buildArray(vector<int>& target, int n);
};
Examples
Example 1:
Input: target = [2], n = 3
Output: ["Push","Pop","Push"]
Explanation: Discard1,keep2,and stop before consuming3.
Example 2:
Input: target = [3], n = 3
Output: ["Push","Push","Pop","Pop","Push"]
Explanation: Push1 and2,pop2 then1,and push3 to reach the target. Immediate push/pop pairs for1 and2 would also be valid.
Constraints
target has length 1 to 100.
1 <= n <= 100
and
1 <= target[i] <= n
.
target is strictly increasing.
Contract considerations
The native result remains vector<string>; validity of the returned operation sequence is the substantive output contract.
What a Strong Answer Covers Guidance
The native result remains vector<string>; validity of the returned operation sequence is the substantive output contract.
Follow-up Questions Guidance
Optional practice extension,separate from the original task: How would you validate an arbitrary returned operation list while enforcing immediate stopping?