leetcode475
public class Solution {
public int FindRadius(int[] houses, int[] heaters) {
houses = houses.Distinct().ToArray();//去重
heaters = heaters.Distinct().ToArray();//去重
var temphouses = houses.Except(houses.Intersect(heaters)).ToArray();
//var tempheaters = heaters.Except(heaters.Intersect(houses)).ToArray();
houses = temphouses;
//heaters = tempheaters;
if (houses.Length == )
{
return ;
}
//将房间与炉子合并到一个列表中
var list = new List<KeyValuePair<int, int>>();//key是坐标,value=0表示房间,value=1表示火炉
foreach (var house in houses)
{
list.Add(new KeyValuePair<int, int>(house, ));
}
foreach (var heater in heaters)
{
list.Add(new KeyValuePair<int, int>(heater, ));
}
list = list.OrderBy(x => x.Key).ToList();//根据坐标排序
var min = int.MinValue;
var minList = new List<int>();
//根据每个房间,找其最近的炉子
for (int i = ; i < list.Count; i++)
{
if (list[i].Value == )
{
var house = list[i].Key;//找到一所房间的坐标
var j1 = i - ;//循环,找前面的第一个炉子
var dis1 = int.MaxValue;
while (j1 >= )
{
if (list[j1].Value == )
{
dis1 = Math.Abs(house - list[j1].Key);
break;
}
j1--;
}
var j2 = i + ;//循环,找后面的第一个炉子
var dis2 = int.MaxValue;
while (j2 < list.Count)
{
if (list[j2].Value == )
{
dis2 = Math.Abs(house - list[j2].Key);
break;
}
j2++;
}
var dis = Math.Min(dis1, dis2);//距离当前房间,最近的炉子的距离
minList.Add(dis);
}
}
min = minList.Max();
return min;
}
}
https://leetcode.com/problems/heaters/#/description
leetcode475的更多相关文章
- [Swift]LeetCode475. 供暖器 | Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- Leetcode475.Heaters供暖器
冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置. ...
随机推荐
- css实现心形图案
用1个标签实现心形图案,show you the code; <!DOCTYPE html> <html lang="en"> <head> & ...
- Springfox与swagger的整合使用(十七)
一.前言 让我们先理一下springfox与swagger的关系. swagger是一个流行的API开发框架,这个框架以“开放API声明”(OpenAPI Specification,OAS)为基础, ...
- 048——VUE中使用animate.css动画库控制vue.js过渡效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DOM之概述
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 认识Applet和Ajax
一.Applet 1.Applet的定义:Applet是采用Java编程语言编写的小应用程序,该程序可以包含在HTML(标准通用标记语言的一个应用)页中,与在页中包含图像的方式大致相同. Java写出 ...
- Win7操作系统安装IE10提示“安装前需要更新与安装程序版本”
安装IE10浏览器时提示错误的 Internet Explorer安装程序版本 故障现象: Win7操作系统在安装IE10浏览器时会弹出对话框,提示错误的Ieternet Explorer 安装程序版 ...
- 故障处理:磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1
按照阿里云官网教程对云服务器进行磁盘扩容,使用fdisk重新分区,最后使用e2fsck和resize2fs来完成文件系统层面的扩容 在执行“e2fsck -f /dev/vdb1”命令时报错,如果你的 ...
- c++ 读取所有图片
copyright by Jun Yang, SUN YAT-SEN UNIVERSITY //FileList.h ///////////////////////////////////////// ...
- MySQL 存储过程理解
/********************************************************************************* * MySQL 存储过程理解 * ...
- notification的创建及应用
之前我用了button.setonclicklistener来获取一个点击事件,但是在new notificationcompat.builder是会报一个没有定义的错误.这种点击事件的方式就不会报那 ...