当前位置:网站首页>【Leetcode】475. Heaters
【Leetcode】475. Heaters
2022-08-01 23:47:00 【记录算法题解】
题目地址:
https://leetcode.com/problems/heaters/description/
给定两个数组 A A A和 B B B,分别代表房屋的坐标和加热器的坐标,每个加热器 B [ i ] B[i] B[i]能加热的范围为 [ B [ i ] − x , B [ i ] + x ] [B[i]-x,B[i]+x] [B[i]−x,B[i]+x], x x x对于所有加热器都是一样的。问半径最小是多少能使得所有房屋都能被加热。
可以二分答案,然后用双指针(假设两个数组都排好了序,那么可以关注每个加热器可以加热到的最后一个房屋,这个下标一定是单调增的)。代码如下:
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int l = 0, r = max(heaters[0] - houses[0], houses.back() - heaters[0]);
while (l < r) {
int mid = l + (r - l >> 1);
if (check(mid, houses, heaters)) r = mid;
else l = mid + 1;
}
return l;
}
bool check(int len, vector<int>& ho, vector<int>& he) {
for (int i = 0, j = 0; i < he.size(); i++) {
if (ho[j] < he[i] - len) return false;
while (j < ho.size() && abs(ho[j] - he[i]) <= len) j++;
if (j == ho.size()) return true;
}
return false;
}
};
时间复杂度 O ( l A log l A + l B log l B + ( l A + l B ) log max { B [ 0 ] − A [ 0 ] , B [ l B − 1 ] − A [ 0 ] } ) O(l_A\log l_A+l_B\log l_B+(l_A+l_B)\log \max\{B[0]-A[0], B[l_B-1]-A[0]\}) O(lAloglA+lBloglB+(lA+lB)logmax{ B[0]−A[0],B[lB−1]−A[0]}),空间 O ( 1 ) O(1) O(1)。
边栏推荐
- Flink Yarn Per Job - 提交流程一
- C language - branch statement and loop statement
- 多御安全浏览器android版更新至1.7,改进加密协议
- 6134. 找到离给定两个节点最近的节点-力扣双百代码
- 程序员还差对象?new一个就行了
- 一款简洁的文件传输工具
- 20220725资料更新
- Architecture basic concept and nature of architecture
- Flink Yarn Per Job - CliFrontend
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
猜你喜欢

使用Jenkins做持续集成,这个知识点必须要掌握

云原生DevOps环境搭建

Making a Simple 3D Renderer

【MySQL篇】初识数据库

oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed

Building a cloud-native DevOps environment

solidity

Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
![[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环

月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
随机推荐
12306抢票,极限并发带来的思考?
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
斜堆、、、
高效工作文档产出归类
2022第六届强网杯部分wp
Use Jenkins for continuous integration, this knowledge point must be mastered
thinkphp漏洞总结
中职网络安全竞赛B7比赛部署流程
洞见云原生微服务及微服务架构浅析
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
qt-faststart installation and use
Programmer is still short of objects? A new one is enough
cdh6打开oozieWeb页面,Oozie web console is disabled.
2022 6th Strong Net Cup Part WP
使用Jenkins做持续集成,这个知识点必须要掌握
How do programmers solve online problems gracefully?
Thymeleaf简介
获取小猪民宿(短租)数据
程序员还差对象?new一个就行了