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. ACM题目————吝啬的国度

    描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设 ...

  2. 计算机学院大学生程序设计竞赛(2015’12)Polygon

    Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  3. c# 服务端

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. C#读取Excel显示到repeater中

    首先需要一个用来存储我们需要显示的内容,防止页面回发丢失(添加时使用) #region 缓存文件 private DataTable excelData; /// <summary> // ...

  5. phpcms 05

    所谓的异步加载,也就是两个线程同时执行一个任务,比如一个加载文字,一个加载图片,这样子可以先看到文字 footer,html包含尾部文件 {template "content",& ...

  6. spoj 3871. GCD Extreme 欧拉+积性函数

    3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. Th ...

  7. hdu 4002 Find the maximum

    Find the maximum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  8. An error occurred while filtering resources-----maven项目报错

    解决办法 需要在pom中设定jdk的版本 <plugins> <!-- compiler插件, 设定JDK版本 --> <plugin> <groupId&g ...

  9. CodeForces 651A Joysticks 贪心

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. 编写Music类

    package a; public class Instrument { public void play() { System.out.println("弹奏乐器"); } } ...