Advertisement
ogv

Untitled

ogv
Sep 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. // 41 ms, runtime beats 53.07 % of java submissions.
  2. class Solution {
  3.     public int[] nextLargerNodes(ListNode head) {        
  4.         ArrayList<Integer> r = new ArrayList<>();
  5.        
  6.         Map<ListNode, Integer> map = new HashMap<>();
  7.        
  8.         ListNode c = head;
  9.         while (c != null) {
  10.             if (!map.containsKey(c)) nextMax(c, map);
  11.            
  12.             r.add(map.get(c));
  13.             c = c.next;
  14.         }
  15.      
  16.         int[] result = new int[r.size()];
  17.         for (int i = 0; i < result.length; i++) result[i] = r.get(i);
  18.         return result;
  19.     }
  20.    
  21.     private ListNode nextMax(ListNode from, Map<ListNode,Integer> map) {
  22.         ListNode next = from.next;
  23.         while (next != null) {
  24.             if (next.val > from.val){
  25.                 map.put(from, next.val);
  26.                 return next;
  27.             }
  28.             next = nextMax(next, map);
  29.         }
  30.         map.put(from, 0);
  31.         return null;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement