heaters
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的更多相关文章
- [LeetCode] Heaters 加热器
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- Leetcode: Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [Swift]LeetCode475. 供暖器 | Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- LeetCode算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- 【leetcode】475. Heaters
problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...
- 475. Heaters
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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 ...
- 『ACM C++』 Codeforces | 1066B - Heaters
今日不写日感,直接扔上今日兴趣点: 新研究称火星曾经有一个巨大的地下水系统 链接:https://mbd.baidu.com/newspage/data/landingsuper?context=%7 ...
随机推荐
- [thml]HTML select标签 获取选中的option的value及Text内容
很简单的select标签: <select id="hello" onchange="getContent(this.value,this.options[this ...
- Javascript中自动切换焦点
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title&g ...
- c#sqlhelper之用法
MySqlParameter[] a=new MySqlParameter[]{new MySqlParameter("@stu_id",stu_id)}; 参数使用
- Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct
1. DistinctEqualityComparer.cs public class DistinctEqualityComparer<T, V> : IEqualityComparer ...
- MySql5.7-多源复制(多主单从)
1.1.主库配置 my.cnf #确保唯一 server-id=1 #作为Master要开启binlog log-bin=mysql-bin #binlog format有三种形式:Stateme ...
- Poj(1220),hash
题目链接:http://poj.org/problem?id=1200 这个题,我真是无限MLE,RE,WA,太伤心了,还是写一下吧.题意很简单(英语很好读),最后看了一下金海峰的思路.果然,应该是我 ...
- 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 80070005
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005.跟踪了一下,结果是将记录导出 ...
- mvcAPI (入门 2)
1)建立一个实体类 using System; using System.Collections.Generic; using System.Linq; using System.Web; names ...
- shell 中的>文件重定向符 和 标准输入、输出、错误以及 2&1 的含义*
http://www.cnblogs.com/chenmh/p/5382044.html 问:其中 的2>&1是怎么回事? . test.sh > test.log 2>&a ...
- ActiveMQ点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...