https://leetcode.com/problems/heaters/

开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过。原因是heater会有重复情况,这时候对于飘红部分就不会往前面继续检查。所以把<改成<=让相同的情况也继续往前面走,走到最后一个,就可以了。Accepted!

package com.company;

import java.util.Arrays;

class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(houses);
Arrays.sort(heaters); int ret = Integer.MIN_VALUE;
int i = 0;
int hlen = heaters.length;
int tmp = Integer.MIN_VALUE;
for (int h: houses) {
while (i+1 < hlen && Math.abs(heaters[i+1]-h) <= Math.abs(heaters[i]-h)) {
i++;
}
if (Math.abs(heaters[i]-h) > ret) {
ret = Math.abs(heaters[i]-h);
}
}
return ret;
}
} public class Main { public static void main(String[] args) { Solution solution = new Solution(); int[] houses = {1, 2, 2, 3, 4, 5};
int[] heaters = {1, 2, 2, 3, 4, 5};
int ret = solution.findRadius(houses, heaters); System.out.printf("Done ret: %d\n", ret); } }

heaters的更多相关文章

  1. [LeetCode] Heaters 加热器

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

  2. Leetcode: Heaters

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

  3. [Leetcode] Binary search -- 475. Heaters

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

  4. [Swift]LeetCode475. 供暖器 | Heaters

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

  5. LeetCode算法题-Heaters(Java实现)

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

  6. 【leetcode】475. Heaters

    problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...

  7. 475. Heaters

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. 475. Heaters (start binary search, appplication for binary search)

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

  9. 『ACM C++』 Codeforces | 1066B - Heaters

    今日不写日感,直接扔上今日兴趣点: 新研究称火星曾经有一个巨大的地下水系统 链接:https://mbd.baidu.com/newspage/data/landingsuper?context=%7 ...

随机推荐

  1. js函数自执行

    在javascript里,任何function在执行的时候都会创建一个执行上下文,因为function声明的变量和function有可能只在该function内部,这个上下文,在调用function的 ...

  2. 每日一九度之题目1016:火星A+B

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5346 解决:1464 题目描述:     读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的, ...

  3. Linux下怎么运行java程序

    在Linux下安装好jdk配置好环境变量后,要回到程序所在的目录下,然后跟在windows一样输入   java (程序名)运行,原理是就好像在Windows的DOS环境下执行java这个命令时必须在 ...

  4. jar的下载地址及其使用说明

    有时候会苦于jar的搜索.这里我就给出我平时用到的吧,方便大家.后期会不断添加. 1.dom4j-1.6.1.jar 主要用于解析xml的jar包.下载地址:   http://pan.baidu.c ...

  5. Behavior Designer中的内置消息机制

    最近在用Behavior Designer,其中需要用到消息机制,看了一下其中自带了这套东西 注册 Owner.RegisterEvent<string>("Message&qu ...

  6. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  7. ios布局约束

    http://blog.csdn.net/pucker/article/details/41843511 除了使用Pin菜单之外,也可以直接在文档结构窗口中或者设计器中通过拖拽建立约束.在文档结构窗口 ...

  8. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  9. HDU 5615 Jam's math problem

    Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...

  10. win7下如何安装JDK

    第一步:点击下载好的JDK安装程序,百度一下,网上很多,然后弹出如下界面,点击“下一步”即可. 2 第二步:此时比较关键,设置安装的路径,可以事先建立一个文件夹,以在D盘为例,如D:\java. 3 ...