leetcode oj java 475. Heaters

来源:互联网 发布:波音市值知乎 编辑:程序博客网 时间:2024/05/21 17:38

一、问题描述:

Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.

Note:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters' warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same.

Example 1:

Input: [1,2,3],[2]Output: 1Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.

Example 2:

Input: [1,2,3,4],[1,4]Output: 1Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

二、解决思路:

首先分别对house和heater的坐标由小到大排序,确定每一个house的距离最近的Heaters,以及距离ki,  然后取所有k中的最大值。

        用一个全局变量表示整体的最小半径min。 针对每一个Houses, 分为三种情况:

case1: 其左边和右边都有heaters, 此时ki 为距离该houses最近的Heater 到house的距离

case2: 只有右边有Heater, 此时ki为house右边第一个Heater到house的距离

case3: 只有左边有heater,此时ki为该house左边(离它最近的)的heater到它的距离

如果ki 大于min 则更新min为ki 

遍历所有的houses ,结束

三、代码:

package T01;import java.util.Set;import java.util.TreeSet;/** * @author 作者 : xcy * @version 创建时间:2017年1月7日 下午11:59:53 *          类说明 */public class t475 {    public static void main(String[] args) {        // TODO Auto-generated method stub        //        int[] houses = { 1, 1, 1, 1, 1, 1, 999, 999, 999, 999, 999 };        int[] houses = { 1, 4 };        int[] heaters = { 500, 900 };        System.out.println(findRadius(houses, heaters));    }    public static int findRadius(int[] houses, int[] heaters) {        houses = removeRepeat(houses);        heaters = removeRepeat(heaters);        int hl = houses.length;        int hel = heaters.length;        if (hel == 1) {            int a = houses[0];            int b = houses[hl - 1];            int c = heaters[0];            if (c < a) {                return b - c;            }            if (c > b) {                return c - a;            } else {                return Math.max(b - c, c - a);            }        }        int min = 0;        int hid = 0;        for (int i = 0; i < hl; i++) {            int left = 0;            int right = 0;            if (heaters[hid] >= houses[i]) {                right = heaters[hid] - houses[i];                left = right;            } else {                while (hid < hel && heaters[hid] < houses[i]) {                    hid++;                }                hid--;                left = houses[i] - heaters[hid];                if (hid == hel - 1) {                    right = left;                } else {                    right = heaters[hid + 1] - houses[i];                }            }            int tmin = left < right ? left : right;            min = min < tmin ? tmin : min;        }        return min;    }    public static int[] removeRepeat(int[] nums) {        Set<Integer> sh = new TreeSet<Integer>();        for (int i : nums) {            sh.add(i);        }        int len = sh.size();        nums = new int[len];        int count = 0;        for (int i : sh) {            nums[count] = i;            count++;        }        return nums;    }}


0 0
原创粉丝点击