当前位置:网站首页>LeetCode_Dec_3rd_Week
LeetCode_Dec_3rd_Week
2022-08-04 05:30:00 【KuoGavin】
December 20th : 475. 供暖器
December 21st : 1154. 一年中的第几天
December 20th : 475. 供暖器
对于每个房屋,要么用前面的暖气,要么用后面的,二者取近的,得到距离;对于所有的房屋,选择最大的上述距离。
这里需要注意的是,对于某个房屋,它只有前面或者是只有后边有暖气,这种情况需要考虑到。
在对边界值进行查找的过程中,常用到二分查找(对于边界的二分,直观看是对有序序列进行对半划分),这里stl algorithm中的upper_bound
和lower_bound
很好用,具体的函数签名如下:

lower_bound对应求得的是第一个不小于value的值所对应的迭代器,也即是大于等于区间的左边界,这样称为lower bound就不难理解了。

同理,upper_bound求得的是不大于value的区间的右边界,也即是第一个大于value的值的迭代器,也即理解为upper bound。
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end()); //将散热器位置排序
int ret = 0;
for(auto house : houses) {
int cur = INT_MAX; //当前房屋所需的最小供热半径
auto right = lower_bound(heaters.begin(), heaters.end(), house); //找到对应的右侧的散热器位置
if(right != heaters.end()) cur = *right - house; //若是右侧有散热器,则更新供热半径
if(right != heaters.begin()) cur = min(cur, house - *(right-1)); //若是左侧也有散热器
ret = max(cur, ret); //最终结果取各个房屋的供热半径的最大值
}
return ret;
}
};
December 21st : 1154. 一年中的第几天
若是dayOfYear作为常驻进程的一部分,并且频繁调用的话,可以在Solution类中开辟一个前缀和数组,记录当前月的前面月份的日期和,若是只是偶尔调用,则当场加即可。
闰年的定义,我都记不清了,难受(摘自百度百科):
- 普通闰年:公历年份是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]);
}
};
边栏推荐
- 动手学深度学习_多层感知机
- MNIST手写数字识别 —— 从零构建感知机实现二分类
- Lee‘s way of Deep Learning 深度学习笔记
- No matching function for call to 'RCTBridgeModuleNameForClass'
- PP-LiteSeg
- 【CV-Learning】Convolutional Neural Network
- Use of double pointers
- makefile基础学习
- [Deep Learning 21-Day Learning Challenge] 3. Use a self-made dataset - Convolutional Neural Network (CNN) Weather Recognition
- 关于DG(域泛化)领域的PCL方法的代码实例
猜你喜欢
Image-Adaptive YOLO for Object Detection in Adverse Weather Conditions
[Deep Learning 21 Days Learning Challenge] Memo: What does our neural network model look like? - detailed explanation of model.summary()
SQL注入详解
双向LSTM
Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers
动手学深度学习_卷积神经网络CNN
Golang环境变量设置(二)--GOMODULE&GOPROXY
基于BiGRU和GAN的数据生成方法
DeblurGAN-v2: Deblurring (Orders-of-Magnitude) Faster and Better 图像去模糊
动手学深度学习__数据操作
随机推荐
target has libraries with conflicting names: libcrypto.a and libssl.a.
SQL注入详解
A code example of the PCL method in the domain of DG (Domain Generalization)
Pytest common plug-in
关于DG(域泛化)领域的PCL方法的代码实例
BatchNorm&&LayerNorm
postgres recursive query
fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程
代码庆端午--粽你心意
Pytorch语义分割理解
"A minute" Copy siege lion log 】 【 run MindSpore LeNet model
Copy攻城狮信手”粘“来 AI 对对联
makefile基础学习
How to grow into a senior engineer?
如何用Pygame制作简单的贪吃蛇游戏
Copy Siege Lion 5-minute online experience MindIR format model generation
Install Minikube Cluster in AWS-EC2
Copy攻城狮5分钟在线体验 MindIR 格式模型生成
tensorRT5.15 使用中的注意点
Windows10重置MySQL用户密码