Java BST Solution worse O(n^2) best O(n)


  • 0
    H
    class Solution {
        public int[] dailyTemperatures(int[] temperatures) {
            int n = temperatures.length;
            int[] result = new int[n];
            TreeMap<Integer, Integer> map = new TreeMap<>();
            for (int i=n-1; i>=0; i--) {
                int t = temperatures[i];
                Map<Integer, Integer> higher = map.tailMap(t, false);
                java.util.Collection<Integer> indices = higher.values();
                if (indices.size() != 0)
                    result[i] = Collections.min(indices) - i;
                map.put(t, i);
            }
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.