java solution using BST


  • 0
    2
    public class Solution {
        public int findRadius(int[] houses, int[] heaters) {
            TreeSet<Integer> ts = new TreeSet<Integer>();
            for (int i : heaters) {
                ts.add(i);
            }
            int r = 0;
            for (int i : houses) {
                Integer ceil = ts.ceiling(i);
                Integer floor = ts.floor(i);
                r = Math.max(r, Math.min(ceil == null ? Integer.MAX_VALUE : ceil - i, floor == null ? Integer.MAX_VALUE : i - floor));
            }
            return r;
        }
    }
    

Log in to reply
 

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