当前位置:网站首页>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]);
}
};
边栏推荐
- Android foundation [Super detailed android storage method analysis (SharedPreferences, SQLite database storage)]
- 动手学深度学习_softmax回归
- Copy Siege Lion 5-minute online experience MindIR format model generation
- 【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
- MFC读取点云,只能正常显示第一个,显示后面时报错
- Copy攻城狮5分钟在线体验 MindIR 格式模型生成
- Amazon Cloud Technology Build On-Amazon Neptune's Knowledge Graph-Based Recommendation Model Building Experience
- Introduction to Convolutional Neural Networks
- Copy攻城狮的年度之“战”|回顾2020
- Endnote编辑参考文献
猜你喜欢

Pytorch问题总结

度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)

【深度学习日记】第一天:Hello world,Hello CNN MNIST

arm交叉编译

PCL1.12 解决memory.h中EIGEN处中断问题
![[Deep Learning Diary] Day 1: Hello world, Hello CNN MNIST](/img/06/6f49260732e5832edae2ec80aafc99.png)
[Deep Learning Diary] Day 1: Hello world, Hello CNN MNIST

Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers

腾讯、网易纷纷出手,火到出圈的元宇宙到底是个啥?

arm-3-中断体系结构

Lee‘s way of Deep Learning 深度学习笔记
随机推荐
Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers
空洞卷积
数据库的简述与常用操作指南
MNIST手写数字识别 —— ResNet-经典卷积神经网络
【论文阅读】Exploring Spatial Significance via Hybrid Pyramidal Graph Network for Vehicle Re-identificatio
Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience
TensorFlow2 study notes: 8. tf.keras implements linear regression, Income dataset: years of education and income dataset
MOOSE平台使用入门攻略——如何运行官方教程的例子
Rules.make-适合在编辑模式下看
[CV-Learning] Convolutional Neural Network Preliminary Knowledge
【论文阅读】Anchor-Free Person Search
Windows10重置MySQL用户密码
No matching function for call to ‘RCTBridgeModuleNameForClass‘
PCL窗口操作
如何用Pygame制作简单的贪吃蛇游戏
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
PyTorch
强化学习中,Q-Learning与Sarsa的差别有多大?
Brief description of database and common operation guide
MNIST手写数字识别 —— 从零构建感知机实现二分类