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供暖器
冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖. 现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径. 所以,你的输入将会是房屋和供暖器的位置. ...
随机推荐
- bzoj1257: [CQOI2007]余数之和 整除分块
题意:给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的值其中k mod i表示k除以i的余数.例如j(5, 3)=3 mod ...
- torch中的多线程threads学习
torch中的多线程threads学习 torch threads threads 包介绍 threads package的优势点: 程序中线程可以随时创建 Jobs被以回调函数的形式提交给线程系统, ...
- bzoj1025
题意: 找环 有多少种不同的排列 使排列数目为n 题解: 考虑dp f[i][j]表示前i个质数,和为j的方案数 然后转移一下即可 代码: #include<bits/stdc++.h> ...
- jacoco生成覆盖率
要统计自动化的覆盖率,所以临时看了下jacoco,记录下过程.后续用的时候方便查看. 1.build.gradle中修改 apply plugin: 'jacoco' android { buildT ...
- 【转】socket 通信简介
转自:http://blog.csdn.net/xiaoweige207/article/details/6211577/ “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是 ...
- 2018.11.23 Cypress BLE module test
CYx63BPA BLE module IQC test guide Test Jig setting:1. Connect USB1 and USB2 with computer serial ...
- 征信接口调用,解析(xml)
数据传输格式报文格式:xml public CisReportRoot queryCisReport(PyQueryBean pyQueryBean) throws Exception { CisRe ...
- 选择语句=》OO函数实现
let a; let b; if (a==="A") { b='定向' }else if (a==='B') { b='开放' }else if(a==='C') { b='全部' ...
- CUDA Samples: matrix multiplication(C = A * B)
以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...
- 首次运行tensorflow-gpu 1.0 报错 failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
发现博客: https://blog.csdn.net/u010752600/article/details/79534910 于是找到解决方法. sudo rm -rf ~/.nv/