This problem looks busy at first, but the accepted solution is built around one steady invariant. For 496. Next Greater Element I, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are nextGreaterElement.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
03
04The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
05
06Example 1:
07Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
08Output: [-1,3,-1]
09Explanation:
10 For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
11 For number 1 in the first array, the next greater number for it in the second array is 3.
12 For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
13Example 2:
14Input: nums1 = [2,4], nums2 = [1,2,3,4].
15Output: [3,-1]
16Explanation:
17 For number 2 in the first array, the next greater number for it in the second array is 3.
18 For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
19Note:
20All elements in nums1 and nums2 are unique.
21The length of both nums1 and nums2 would not exceed 1000.
22**/
23
24//Runtime: 12 ms, faster than 99.57% of C++ online submissions for Next Greater Element I.
25//Memory Usage: 9.1 MB, less than 89.68% of C++ online submissions for Next Greater Element I.
26class Solution {
27public:
28 vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
29 vector<int> ans;
30 for(int findNum : findNums){
31 vector<int>::iterator it = find(nums.begin(), nums.end(), findNum);
32 if(it==nums.end() or it==nums.end()-1){
33 ans.push_back(-1);
34 }else{
35 int i = 0;
36 for(i = it-nums.begin(); i < nums.size(); i++){
37 if(nums[i] > findNum){
38 ans.push_back(nums[i]);
39 break;
40 }
41 }
42 if(i==nums.size()){
43 ans.push_back(-1);
44 }
45 }
46 }
47 return ans;
48 }
49};
Cost