I like to read this solution as a small machine: keep the useful information, throw away the noise. For 946. Validate Stack Sequences, the solution in this repository is mainly a stack solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack, greedy.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are validateStackSequences.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 8 ms, faster than 90.81% of C++ online submissions for Validate Stack Sequences.
02//Memory Usage: 6.9 MB, less than 100.00% of C++ online submissions for Validate Stack Sequences.
03class Solution {
04public:
05 bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
06 int i = 0, j = 0;
07 stack<int> stk;
08 int last = -1;
09
10 //jump out when we cannot push or pop the stack
11 while((i < pushed.size() || j < popped.size()) && (stk.size() != last)){
12 last = stk.size();
13 cout << i << " " << j << " " << endl;
14 if(!stk.empty() && stk.top() == popped[j]){
15 stk.pop();
16 // cout << "pop " << popped[j] << endl;
17 j++;
18 }else if(i < pushed.size()){
19 stk.push(pushed[i]);
20 // cout << "push " << pushed[i] << endl;
21 i++;
22 }
23 }
24
25 return stk.size() == 0;
26 }
27};
28
29//Approach 1: Greedy
30//Runtime: 8 ms, faster than 90.81% of C++ online submissions for Validate Stack Sequences.
31//Memory Usage: 7 MB, less than 100.00% of C++ online submissions for Validate Stack Sequences.
32//time: O(N), space: O(N)
33class Solution {
34public:
35 bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
36 int N = pushed.size();
37 stack<int> stk;
38
39 int j = 0; //index to popped
40 for(int x : pushed){
41 stk.push(x);
42 //pop once we can do that, greedy algorithm
43 while(!stk.empty() && j < N && stk.top() == popped[j]){
44 stk.pop();
45 j++;
46 }
47 }
48
49 return j == N;
50 }
51};
Cost