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供暖器
冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置. ...
随机推荐
- Appium 自动化测试(6) -- 使用Appium操作YY语音例子
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import unittest from appium import webdriver ...
- Leetcode 52
//N皇后的基础上改了一点class Solution { public: int totalNQueens(int n) { ; vector<); DFS(pos,,res); return ...
- 动态创建OATipBean
动态创建OATipBean. 动态创建的OATipBean无法直接设置提示内容,需要添加一个静态文本. 参考User Guide示例如下. If you need to create a tip pr ...
- 用setTimeout实现setInterval函数
最近get一个新知识,也不算是新知识,可能是以前自己没有认真对待(对自己无语ing,si不si傻). 废话不多说,直接来看代码吧 function setInterval(func, t){ var ...
- Loops with PL/SQL
1. Basic loop loop /* statements */ end loop; 2. While loop while a > b loop /* statements */ end ...
- 【LeetCode 38_字符串_算术运算】Count and Say
string countAndSay(int n) { string res; ) return res; res = "; ) { int len = res.size(); int i, ...
- eclipse 编码设置【转】
一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样以后新建的文件也是新设置的编码格式. eclipse->window->p ...
- New Concept English Two 28 76
$课文74 舞台之外 784. An ancient bus stopped by a dry river bed and a party of famous actors and actresse ...
- Linux:提示符PS1个性设置
提示符PS1个性设置 1)默认PS1 echo $PS1 2)个性PS1 #去掉了默认显示的[]号#\e[1;34m\]\u:user名高亮显示并显示颜色#\e[5;33m\]\h:hostname主 ...
- LambdaMART简介——基于Ranklib源码(一 lambda计算)
学习Machine Learning,阅读文献,看各种数学公式的推导,其实是一件很枯燥的事情.有的时候即使理解了数学推导过程,也仍然会一知半解,离自己写程序实现,似乎还有一道鸿沟.所幸的是,现在很多主 ...