题目:

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.

链接:https://leetcode.com/problems/heaters/#/description

3/28/2017

18ms, beat 96%

每个房屋只遍历一遍,heater却可能需要在update house之后重新来算。改正的次数太多。最主要的改变是:

1. radis的初始值,如果第一个house在第一个heater左边则至少大于这个距离;但是如果在heater右边,就跟其他房间/heater计算方法一样

2. 29行还是需要更新radis的,否则一旦heater前进之后,我们不会根据左边的house来更新radis的。如果左边在下一个heater范围内,一定要在更新heater index之前计算

3. 26, 29行不要逼着眼更新,一定要选跟当前radis中较大的那个

4. 注意for循环中没有步进,或者可以改成while loop

 1 public class Solution {
 2     public int findRadius(int[] houses, int[] heaters) {
 3         Arrays.sort(houses);
 4         Arrays.sort(heaters);
 5         int index = 0;
 6         int radis = 0;
 7         if (houses[0] < heaters[0]) radis = heaters[0] - houses[0];
 8         for (int i = 0; i < heaters.length; ) {
 9             // all unvisited houses left to heater[i] should be less than current radis
10             // some unvisited houses right to heater[i] may also less than current radis
11             // break when 1. invalid index, 2. houses right to heater[i] larger than current radis
12             while (index < houses.length && (houses[index] <= heaters[i] || houses[index] - heaters[i] <= radis)) index++;
13             if (index == houses.length) break;
14             // have unvisited heaters
15             // here we meet first house out of range on the right
16             if (i < heaters.length - 1) {
17                 // house is right to next heater, we could update radis when processing next heater
18                 // because this house will still break in while loop
19                 if (heaters[i + 1] <= houses[index]) {
20                     i++;
21                 } else {
22                     // next heater is right to current house, the house has to be in the range of current heater,
23                     // need to update radis
24                     // do not update heater index, we may meet more houses have to be covered by this heater
25                     if (heaters[i + 1] - houses[index] > houses[index] - heaters[i]) {
26                         radis = Math.max(houses[index] - heaters[i], radis);
27                     } else {
28                         // next heater will cover current house
29                         radis = Math.max(heaters[i + 1] - houses[index], radis);
30                         i++;
31                     }
32                     // radis determined by current house is done
33                     index++;
34                 }
35             } else {
36                 // no unvisited heaters, this last house out of range of current radis, update radis
37                 radis = houses[houses.length - 1] - heaters[i];
38                 break;
39             }
40         }
41         return radis;
42     }
43 }

还需要看别人的算法

别人的算法,巧妙的运用了Arrays.binarySearch()方法的返回值,其中第8行运用了负数的反码:(-(insertion point) - 1)。看来Java也是有意用反码,返回的是在数组中可以插入的第几个值,返回的是下标的相反数?

还有一点,如果本身返回的是正数,那么没有必要来计算,因为result = 0已经足够了。

第9,10行就是边界问题的考虑。时间复杂度O((m+n)lgn)

 1 public int findRadius(int[] houses, int[] heaters) {
 2     Arrays.sort(heaters);
 3     int result = 0;
 4
 5     for (int house : houses) {
 6         int index = Arrays.binarySearch(heaters, house);
 7         if (index < 0) {
 8             index = ~index;
 9             int dist1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
10             int dist2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
11
12             result = Math.max(result, Math.min(dist1, dist2));
13         }
14     }
15
16     return result;
17 }

另外一种算法,还是不理解:https://discuss.leetcode.com/topic/71450/simple-java-solution-with-2-pointers

我猜测的原因是,目的是每个house都被取暖,所以按照house来遍历肯定可以保证都被cover了,而且它只跟左右的来比较

 1 public class Solution {
 2     public int findRadius(int[] houses, int[] heaters) {
 3         Arrays.sort(houses);
 4         Arrays.sort(heaters);
 5
 6         int i = 0, res = 0;
 7         for (int house : houses) {
 8             while (i < heaters.length - 1 && heaters[i] + heaters[i + 1] <= house * 2) {
 9                 i++;
10             }
11             res = Math.max(res, Math.abs(heaters[i] - house));
12         }
13
14         return res;
15     }
16 }

更多讨论:

https://discuss.leetcode.com/category/606/heaters

转载于:https://www.cnblogs.com/panini/p/6637810.html

475. Heaters相关推荐

  1. leetcode 475. Heaters | 475. 供暖器(找最后一个不大于target的值/第一个不小于target的值)

    题目 https://leetcode.com/problems/heaters/ 题解 class Solution {public int findRadius(int[] houses, int ...

  2. LeetCode 475. Heaters

    This problem reflects a key strategy when dealing with discrete problem space - split split split.. ...

  3. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  4. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  5. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  6. 寒假LeetCode打卡

    文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. leetcode 用java_LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  9. AI一分钟 | Windows负责人离职;华为2017年收入6036亿元,净利475亿元

    整理 | SuiSui 出品 | AI科技大本营(公众号ID:rgznai100) 一分钟AI 因亚利桑那州致命事故尚未解决,Uber放弃加州自动驾驶汽车测试权,不再续办牌照 华为公布2017年财报: ...

最新文章

  1. Java项目:学生信息管理系统(java+SSM+jsp+mysql+maven)
  2. Android APP全面屏适配技术要点
  3. 互联网产品各阶段的标准流程文档
  4. 皮一皮:钢铁直女?鉴定了,钢的不能再钢!
  5. oracle中的decode的使用
  6. 推荐系统——矩阵分解FM
  7. Normalize.css用法
  8. oracle登录无法处理服务名,ORA-12154: TNS: 无法处理服务名 plsql能登陆
  9. php 0 n随机数,PHP n个不重复的随机数生成代码
  10. 运放放大倍数计算公式_运放电路设计【1】
  11. 创业工场麦刚:不要把创业美化
  12. apscheduler任务配置信息,实现100%数据库化
  13. Linux进程中的RSS和VSZ
  14. 掌门教育三大举措落实个性化教学 让“因材施教”落到实处
  15. MATLAB 与 音频处理 相关内容摘记
  16. 微信小程序如何修改小程序名称
  17. 小程序一键生成系统网站源码
  18. 图像超分辨率重构实战
  19. Pedometer_forAndroid
  20. c++ 正则判断是否是数值包括负数、小数、整数

热门文章

  1. 多态amp;nbsp;OC——第十天
  2. 【转】C# typeof()实例详解
  3. 可怕又可笑的看病经历
  4. mysql 导入单个表_MySQL 备份恢复(导入导出)单个 innodb表
  5. 精确记算程序的运行时间或者某段代码的运行时间
  6. 【LDA学习系列】Gibbs采样python代码
  7. 【Python学习系列二十三】Scikit_Learn库降维方法(矩阵分解)-PCAFA
  8. 机器学习笔记(一)绪论
  9. 搭建Java ME 开发环境
  10. Android中BaseAdapter使用总结(imooc笔记)