[leetcode]475. Heaters

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: 1
Explanation: 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: 1
Explanation: 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,对每个house,在取暖器数组中进行binary search,如果命中,则说明取暖器位置和house位置重合,这个house的最小半径为0;如果没有命中,则使用返回的index,将index左边和右边的取暖器坐标与house坐标求差值,找出这个house最小半径。说白了,也是在查找house的最近左右取暖器。

public int findRadius(int[] houses, int[] heaters) {
		Arrays.sort(heaters);
		
		int result = 0;
		for(int house:houses) {
			int index = Arrays.binarySearch(heaters, house);
			if(index<0) { //index<0,则说明在headers中没有该house,返回 (-(插入点) - 1),第一个大于此键的元素索引  
				index = ~index;
				int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
	                        int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;

	                        result = Math.max(result, Math.min(dist1, dist2));
			}
			//index>=0,则说明在headers中找到了该house, 则说明取暖器位置和house位置重合,该house的最小半径为0,result不变
		}
		return result;
	}

 知识点:

1. Arrays.binarySearch();的用法。
public static int binarySearch(Object[] a,Object key)
     使用二分搜索法来搜索指定数组,以获得指定对象。在进行此调用之前,     必须根据元素的自然顺序对数组进行升序排序(通过 sort(Object[]) 方     法     )。如果没有对数组进行排序,则结果是不确定的。(如果数组包     含不可相互比较的元素(例如,字符串和整数),则无法 根据其元素的自     然顺序对数组进行排序,因此结果是不确定的。)如果数组包含多个等于     指定对象的元素,则无法保证找到的是哪一个。

参数:
      a - 要搜索的数组
      key - 要搜索的值

返回:
     如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1) 

     。插入点 被定义为将键插入数组的那一点:即第一       个大于此键的

     元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意     ,这保证了当且仅当此键被找到时,返回的值将 >= 0。

      否则返回 (-(插入点) - 1)这句话要注意:要是查询的的值小于数组里面

       的最小值那么结果(-(0)-1结果就是-1),如果查询的 值大于数组里面的       最大值。那么结果就是(-(它的索引值)-1结果就是-(1+索引值))


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值