TreeSet is a good thing, I love it!


  • 1
    C
    public class Solution {
        public int findRadius(int[] houses, int[] heaters) {
            TreeSet<Integer> treeSet=new TreeSet<>();
            for (int h: heaters) treeSet.add(h);
            int max=0;
            for (int h: houses) {
                Integer l=treeSet.floor(h), r=treeSet.higher(h);
                max=Math.max(max, Math.min(l==null?Integer.MAX_VALUE:h-l, r==null?Integer.MAX_VALUE:r-h));
            }
            return max;
        }
    }
    

  • 0
    W

    What's the time and space complexity in TreeMap? O(logn)?


  • 0
    C

    @w2qiao Yeah, O(logn) time and O(n) space. It's a BST, I think.


Log in to reply
 

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