475.Heaters java

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.

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.

按照题目要求来看,就是在heaters上放置炉子,覆盖全部的houses,求距离最近炉子的最大半径。思路:一个house可以选择两个炉子。左边的和右边的,哪个距离自己近,就选择被哪个炉子温暖,注意第一个heater左边的houses只能被第一个heater温暖,最后一个heater右边的houses只能被最后一个heater温暖,其余中间的房子,均可以被左边和右边的同时温暖,在所有的最近的半径里面选择最大的半径,就是题目要求求出的半径。java代码:
public class Solution {
    public int findRadius(int[] houses, int[] heaters) {
            Arrays.sort(houses);
       Arrays.sort(heaters);
       int max=0;
       for(int i=0;i<houses.length;i++)
       {
    	   //二分法找出第一个大于houses[i]的heater[mid]
    	   int left=0;
    	   int right=heaters.length-1;
    	   while(left<right)//for循环是用来求解距离自己最近的heater
    	   {
	    	   int mid=left+(right-left)/2;
	    	   if(houses[i]>heaters[mid])
	    	   {
	    		   left=mid+1;
	    	   }
	    	   else
	    		   right=mid;
    	   }
    	   if(right==0)//如果是第一个heater的话 距离就是用这个炉子减去房间的距离
    		   max=Math.max(max,Math.abs(heaters[right]-houses[i]));
    	  else {//其余的话,都是和上一个炉子和这个炉子减去房间的距离,选择其中最小的
    			   int distance1=Math.abs(heaters[right]-houses[i]);
    	    	   int distance2=Math.abs(houses[i]-heaters[right-1]);
    	    	   max=Math.max(max, Math.min(distance1, distance2));//在所有的距离里面选择最大值
			}

       }
       return max;
    }
}

  

 
时间: 2024-10-13 16:09:17

475.Heaters java的相关文章

475. Heaters

http://www.cnblogs.com/EdwardLiu/p/6197086.html https://leetcode.com/problems/heaters/#/description public int findRadius(int[] houses, int[] heaters) { if (houses == null || houses.length == 0) { return 0; } Arrays.sort(houses); Arrays.sort(heaters)

LeetCode #475 Heaters

Question 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

Tomcat #无法启动8005端口

配置tomcat的时候 发现了一个问题,tomcat启动的时候,8005端口未启动,故无法关闭tomcat,后经查询解决了,记录一下子       tomcat启动的时候看不出异常,关闭的时候回报错类似: Jul 17, 2015 9:47:54 AM org.apache.catalina.startup.Catalina stopServer SEVERE: Could not contact localhost:8005. Tomcat may not be running. Jul 17

第四周PSP&amp;进度条

团队项目PSP 一:表格     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论开发环境.工具以及技术 8:37 10:42 25 100 120 分析与设计 分析现有同类软件.设计网页界面 11:12 16:52 45 295 240 编码 具体编码 9:14 次日19:17 960 1020 900 调试运行 修改代码.代码复审 20:32 22:54 36 106 90 总结 总结结果 23:36 23:58 0 22 30

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

leetcode 475. 供暖器(Heaters)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置.你将输出供暖器的最小加热半径. 说明: 给出的房屋和供暖器的数目是非负数且不会超过 25000. 给出的房屋和供暖器的位置均是非负数且不会超过109. 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖. 所有供暖器都遵循你的半

Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity

1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help | start | stop } 2014-7-12 19:41:17 org.apache.catalina.core.AprLifecycleListener init 信息: Loaded APR based Apache Tomcat Native library 1.1.29 usi

Rhythmk 一步一步学 JAVA (21) JAVA 多线程

1.JAVA多线程简单示例 1.1 .Thread  集成接口 Runnable 1.2 .线程状态,可以通过  Thread.getState()获取线程状态: New (新创建) Runnable (可以运行) Blocked  (被阻塞) Waiting  (等待) Timed waiting (计时等待) Terminated  (被终止) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27