2024/11/24 灵茶题单 单调栈
This commit is contained in:
parent
999996234e
commit
ef5f3e6378
|
@ -0,0 +1,22 @@
|
|||
package com.cool.ling_cha_mount.stack;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Num496 {
|
||||
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int j : nums2) {
|
||||
while (!stack.isEmpty() && stack.peek() < j) {
|
||||
map.put(stack.pop(), j);
|
||||
}
|
||||
stack.push(j);
|
||||
}
|
||||
for (int i = 0; i < nums1.length; i++) {
|
||||
nums1[i] = map.getOrDefault(nums1[i], -1);
|
||||
}
|
||||
return nums1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue