I like to read this solution as a small machine: keep the useful information, throw away the noise. For 503. Next Greater Element II, 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.
The notes already sitting in the source point us in the right direction:
- time: O(N^2), space: O(N)
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 nextGreaterElements.
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//time: O(N^2), space: O(N)
02//Runtime: 260 ms, faster than 8.26% of C++ online submissions for Next Greater Element II.
03//Memory Usage: 11.1 MB, less than 100.00% of C++ online submissions for Next Greater Element II.
04
05class Solution {
06public:
07 vector<int> nextGreaterElements(vector<int>& nums) {
08 int N = nums.size();
09 vector<int> ans(N, -1);
10
11 for(int i = 0; i < N; i++){
12 for(int j = i+1; j < i+N; j++){
13 if(nums[j%N] > nums[i]){
14 ans[i] = nums[j%N];
15 break;
16 }
17 }
18 }
19
20 return ans;
21 }
22};
23
24//Approach #3 Using Stack
25//monotonic stack
26//time: O(n), space: O(n)
27//Runtime: 88 ms, faster than 78.59% of C++ online submissions for Next Greater Element II.
28//Memory Usage: 11.9 MB, less than 100.00% of C++ online submissions for Next Greater Element II.
29class Solution {
30public:
31 vector<int> nextGreaterElements(vector<int>& nums) {
32 int N = nums.size();
33 vector<int> ans(N);
34 stack<int> stk;
35 /*
36 two pass:
37 first pass: 2*N-1 -> N, "ans" is filled with next greater element of a non-circular array
38 second pass: N-1 -> 0, now circularity is considered since the greater elements from last pass will remain in the stack
39 */
40 for(int i = 2*N-1; i >= 0; i--){
41 while(!stk.empty() && nums[stk.top()] <= nums[i%N]){
42 stk.pop();
43 }
44 ans[i%N] = stk.empty() ? -1 : nums[stk.top()];
45 stk.push(i%N);
46 }
47 return ans;
48 }
49};
Cost