[Copy question]:
You are given arrays (without duplicates) and nums1
nums2
where's nums1
elements is subset of nums2
. Find all the next greater numbers for nums1
' s elements in the corresponding places of nums2
.
The Next Greater number of a number x in was the first Greater number to their right in nums1
nums2
. If It does not exist, output-1 for this number.
Example 1:
INPUT:NUMS1 = [4,1,2], NUMS2 = [1,3,4,2]. Output: [ -1,3,-1]explanation: for number 4 in the first array, cannot find the next greater number for it in the S Econd array, so output-1. For number 1 with the first array, the next greater number for it in the second array is 3. For number 2 in the first array, there are no next greater number for it in the second array, so output-1.
Example 2:
INPUT:NUMS1 = [2,4], NUMS2 = [1,2,3,4]. Output: [3,-1]explanation: for number 2 in the first array, the next greater number for it in the second array is 3.
for number 4 in the first array, there are no next greater number for it in the second array, so output-1.
[Brute force solution]:
Time Analysis:
Spatial Analysis:
[After optimization]:
Time Analysis:
Spatial Analysis:
[Wonderful output CONDITIONS]:
[Wonderful corner case]:
[Thinking questions]:
Feel the problem with stack is very difficult, basic backrest. Summarized below.
[a sentence of thought]:
[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):
[Drawing]:
[One brush]:
[Two brushes]:
[Three brushes]:
[Four brushes]:
[Five brushes]:
[Results of five-minute visual debug]:
[Summary]:
[Complexity]:time Complexity:o () Space complexity:o ()
[English data structure or algorithm, why not other data structures or algorithms]:
[Key templating code]:
[Other solutions]:
[Follow up]:
Next Greater Element II still uses a stack
[The problem given by the LC becomes variable]:
[Code Style]:
496. Next Greater element I Another array that corresponds to larger elements