496. Next Greater Element I
MA:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] nums3=new int[nums1.length];
for(int i=0;i<nums1.length;i++){
int max=-1;
nums3[i]=max;
int target=nums1[i];
boolean findTarget=false;
for(int j=0;j<nums2.length;j++){
if(nums2[j]==nums1[i] && findTarget==false){
findTarget=true;
continue;
}
if(nums2[j]>target&&findTarget==true){
max=nums2[j];
break;
}
}
findTarget=false;;
nums3[i]=max;
}
return nums3;
}
}
OA:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] nums3=new int[nums1.length];
for(int i=0;i<nums1.length;i++){
int max=-1;
nums3[i]=max;
int target=nums1[i];
boolean findTarget=false;
for(int j=0;j<nums2.length;j++){
if(nums2[j]==nums1[i] && findTarget==false){
findTarget=true;
continue;
}
if(nums2[j]>target&&findTarget==true){
max=nums2[j];
break;
}
}
findTarget=false;;
nums3[i]=max;
}
return nums3;
}
}
OA:
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x
Stack<Integer> stack = new Stack<>();
for (int num : nums) {
while (!stack.isEmpty() && stack.peek() < num)
map.put(stack.pop(), num);
stack.push(num);
}
for (int i = 0; i < findNums.length; i++)
findNums[i] = map.getOrDefault(findNums[i], -1);
return findNums;
}
Suppose we have a decreasing sequence followed by a greater number
For example [5, 4, 3, 2, 1, 6]
then the greater number 6
is the next greater element for all previous numbers in the sequence
We use a stack to keep a decreasing sub-sequence, whenever we see a number x
greater than stack.peek()
we pop all elements less than x
and for all the popped ones, their next greater element is x
For example [9, 8, 7, 3, 2, 1, 6]
The stack will first contain [9, 8, 7, 3, 2, 1]
and then we see 6
which is greater than 1
so we pop 1 2 3
whose next greater element should be 6
评论
发表评论