当前位置:网站首页>LeetCode_Dec_3rd_Week
LeetCode_Dec_3rd_Week
2022-08-04 06:29:00 【KuoGavin】
December 20th : 475. 供暖器
December 21st : 1154. 一年中的第几天
December 20th : 475. 供暖器
对于每个房屋,Either use the front heater,Either use the latter,the two are close,得到距离;for all houses,Choose the largest of the above distances.
这里需要注意的是,for a house,It has heating only in the front or only in the back,This situation needs to be considered.
during the search for boundary values,Commonly used in binary search(For the bisection of the boundary,Intuitively, it is to divide the ordered sequence in half),这里stl algorithm中的upper_bound和lower_bound很好用,具体的函数签名如下:

lower_boundCorrespondingly, the first one is not less thanvalueThe iterator corresponding to the value of ,That is, greater than or equal to the left boundary of the interval,这样称为lower bound就不难理解了.

同理,upper_boundWhat is sought is not greater thanvaluethe right boundary of the interval,That is, the first greater thanvalue的值的迭代器,That is to sayupper bound.
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end()); //Sort the heat sink locations
int ret = 0;
for(auto house : houses) {
int cur = INT_MAX; //The minimum heating radius required for the current house
auto right = lower_bound(heaters.begin(), heaters.end(), house); //Find the corresponding radiator location on the right
if(right != heaters.end()) cur = *right - house; //If there is a radiator on the right side,Then update the heating radius
if(right != heaters.begin()) cur = min(cur, house - *(right-1)); //If there is a radiator on the left side as well
ret = max(cur, ret); //The final result takes the maximum value of the heating radius of each house
}
return ret;
}
};
December 21st : 1154. 一年中的第几天
若是dayOfYearas part of the resident process,and frequently called,可以在SolutionOpen up a prefix and array in the class,Records the date sum of the month preceding the current month,If only called occasionally,You can add it on the spot.
闰年的定义,我都记不清了,难受(摘自百度百科):
- 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年).
- 世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)
class Solution {
public:
int dayOfYear(string date) {
vector<int> days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i = 1; i <= 12; ++i) days[i] += days[i-1];
int year = atoi(date.substr(0, 5).c_str());
int month = atoi(date.substr(5, 3).c_str());
int day = atoi(date.substr(8, 2).c_str());
cout << year << " " << month << " " << day << endl;
return day +
((((year % 400 == 0 && year % 100 == 0) || (year % 100 != 0 && year % 4 == 0)) && month > 2) ?
days[month-1] + 1 : days[month-1]);
}
};
边栏推荐
猜你喜欢

Golang环境变量设置(二)--GOMODULE&GOPROXY

LeetCode_Nov_3rd_Week

详解近端策略优化

典型CCN网络——efficientNet(2019-Google-已开源)

基于asp.net的法律援助平台的设计与实现(附项目链接)

CSDN大礼包--高校圆桌派大礼包

Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience

集合--LinkedList

target has libraries with conflicting names: libcrypto.a and libssl.a.

光条中心提取方法总结(一)
随机推荐
tensorRT教程——tensor RT OP理解(实现自定义层,搭建网络)
(导航页)OpenStack-M版-双节点手工搭建-附B站视频
浅谈游戏音效测试点
深度确定性策略梯度(DDPG)
MNIST手写数字识别 —— 图像分析法实现二分类
Lee‘s way of Deep Learning 深度学习笔记
tensorRT教程——使用tensorRT OP 搭建自己的网络
target has libraries with conflicting names: libcrypto.a and libssl.a.
arm-3-中断体系结构
[开发杂项][编辑器][代码阅读]ctags&vim
LeetCode_22_Apr_2nd_Week
软著撰写注意事项
[开发杂项][调试]debug into kernel
LeetCode_Nov_1st_Week
Golang环境变量设置(二)--GOMODULE&GOPROXY
【深度学习日记】第一天:Hello world,Hello CNN MNIST
Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
打金?工作室?账号被封?游戏灰黑产离我们有多近
深度学习理论——过拟合、欠拟合、正则化、优化器
【论文阅读】TransReID: Transformer-based Object Re-Identification