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 ...
随机推荐
- Maven invalid task...
执行maven构建项目报错: Invalid task '‐DgroupId=*': you must specify a valid lifecycle phase, or a goal in th ...
- JavaScript DOM 编程艺术(第2版)读书笔记(6)
案例研究:图片库改进版 我们在学校里学过一种理论,叫做结构化程序设计.其中有这样一条原则:函数应该只有一个入口和一个出口.从理论上讲,我很赞同这项原则:但在实际工作中,过分拘泥于这项原则往往会使代码变 ...
- 关闭用miniUI打开的窗口
miniUI打开的窗口用window.close关闭无效, 应该用window.CloseOwnerWindow();
- UML类图几种关系的总结(转)
原文:http://gjhappyyy.iteye.com/blog/1422515 在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization), ...
- Entity Framework 第三篇 实体特性声明
Entity Framework中对实体的特性声明有着严格的要求 1.实体必须要有主键特性,但是如果实体没有主键特性那怎么办? public int ExecuteSqlCommand(string ...
- ContentProvider官方教程(6)provider支持的数据类型
Provider Data Types Content providers can offer many different data types. The User Dictionary Provi ...
- 实现Action类
实现Action类 1.Action类的作用: (1)封装HTTP的请求参数: (2)处理用户请求: (3)封装处理结果. 2.Action类是什么,在Action类中应该包含什么: Action类就 ...
- Web的Ajax应用开发模式(一)——了解Ajax的使用形式
寄语: 前天在查看一些公司的招聘要求时,看到有公司要求测试人员了解Ajax,故写此博文旨在帮助测试人员提高自身技术知识水平,愿与广大测试同胞共同进步.(欢迎纠错!!!) 图1: 图2:
- LTMP手动编译安装以及全自动化部署实践(附详细代码)
大家使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(N ...
- 用于 ‘Suse‘ Linux 包管理的 Zypper 命令大全
SUSE( Software and System Entwicklung,即软件和系统开发.其中‘entwicklung‘是德语,意为开发)Linux 是由 Novell 公司在 Linux 内核基 ...