Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] nextGreaterElements(int[] nums) {
- int len = nums.length;
- int[] results = new int[len];
- Arrays.fill(results, -1);
- PriorityQueue<Element> waiting = new PriorityQueue<>();
- waiting.add(new Element(0, nums[0]));
- for (int k = 1; k < 2*len; k++) {
- Element top = waiting.peek();
- if (top.position <= k - len) {
- waiting.poll();
- top = waiting.peek();
- }
- int current = nums[k >= len ? k-len: k];
- if (current > top.value) {
- for (Element element: waiting) {
- int p = element.position;
- results[p >= len ? p-len: p] = current;
- }
- waiting.clear();
- }
- waiting.add(new Element(k, current));
- }
- return results;
- }
- private class Element implements Comparable<Element> {
- public int position;
- public int value;
- public Element(int position, int value) {
- this.position = position;
- this.value = value;
- }
- public int compareTo(Element another) {
- return value - another.value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement