|
@@ -1,9 +1,27 @@
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Base64;
|
|
import java.util.BitSet;
|
|
import java.util.BitSet;
|
|
|
|
+import java.util.Stack;
|
|
import java.util.concurrent.*;
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
public class tmp {
|
|
public class tmp {
|
|
public static void main(String[] args) throws InterruptedException {
|
|
public static void main(String[] args) throws InterruptedException {
|
|
|
|
+ int[] ints = {2, 1, 2, 4, 3};
|
|
|
|
+ nextGreaterElement(ints);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ public static int[] nextGreaterElement(int[] nums) {
|
|
|
|
+ int n = nums.length;
|
|
|
|
+ int[] res = new int[n];
|
|
|
|
+ Stack<Integer> stack = new Stack<>();
|
|
|
|
+ for (int i = n - 1; i >= 0; i--) {
|
|
|
|
+ while (!stack.isEmpty() && stack.peek() <= nums[i]) {
|
|
|
|
+ stack.pop();
|
|
|
|
+ }
|
|
|
|
+ res[i] = stack.isEmpty() ? -1 : stack.peek();
|
|
|
|
+ stack.push(nums[i]);
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
}
|
|
}
|