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的更多相关文章

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

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

  2. Leetcode475.Heaters供暖器

    冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置. ...

随机推荐

  1. express 调优的一个过程和心得,不错的文章

    Netflix的软件工程师Yunong Xiao最近在公司的技术博客上写了一篇文章,分析了他所在的团队在将Netflix网站UI转移到Node.js上时遇到的延迟问题.在文章中他描述了找到问题根本原因 ...

  2. UVALive-5095 Transportation (最小费用流+拆边)

    题目大意:有n个点,m条单向边.要运k单位货物从1到n,但是每条道路上都有一个参数ai,表示经这条路运送x个单位货物需要花费ai*x*x个单位的钱.求最小费用. 题目分析:拆边.例如:u到v的容量为5 ...

  3. wikioi 1035 火车停留 裸费用流

    链接:http://wikioi.com/problem/1035/ 怎么说呢,只能说这个建图很有意思.因为只有m条道,然后能互相接在一起的连通,对每个点进行拆点,很有意思的一道裸费用留题. 代码: ...

  4. Java9新特性

    转载:http://blog.csdn.net/qq_32524177/article/details/77014757 写在前面的话:Java9来了,搜索了很多关于Java9的新特性,但文献不多,特 ...

  5. Word 开发资料集合

    Word 对象模型概述  https://msdn.microsoft.com/zh-cn/library/kw65a0we.aspx DSOframer微软官方API的查阅方法  http://sh ...

  6. [转载]MySQL索引原理与慢查询优化

    好文,以防丢失,故转之,另对排版做简单优化.原文地址:http://ourmysql.com/archives/1401 索引目的 索引的目的在于提高查询效率,可以类比字典,如果要查"mys ...

  7. query更多的筛选用法

    比较操作符$eq : =  写法:db.class0.find({age:{$eq:18}},{name:1,_id:0}); 查找年龄等于18$lt :<$lte : <=$gt : & ...

  8. C语言Socket编程(计算机网络作业)

    最近我计算机网络课程要做作业了,没办法跟着老师一步一步的写C语言的代码,使用的计算就是Socket通信发送消息:代码实现的功能很简单,客户端向服务器端发送消息,服务器端接收客户端发来的消息,并且输出显 ...

  9. Oracle基本操作命令

    一.Oracle监听命令 1.启动监听 lsnrctl start 五.Oracle 查看表空间的大小及使用情况sql语句 --1.查看表空间的名称及大小 SELECT t.tablespace_na ...

  10. oracle之 关闭透明大页

    方法一: 1.设置/etc/grub.conf文件,添加 transparent_hugepage=never ,在系统启动是禁用 [root@hbdw1 ~]# cat /etc/grub.conf ...