当前位置:网站首页>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]);
}
};
边栏推荐
- 语音驱动嘴型与面部动画生成的现状和趋势
- 图像形变(插值方法)
- Code to celebrate the Dragon Boat Festival - Zongzi, your heart
- Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience
- Copy Siege Lions "sticky" to AI couplets
- 动手学深度学习__数据操作
- arm交叉编译
- Unity ML-agents 参数设置解明
- TypeError: load() missing 1 required positional argument: ‘Loader‘
- PCL1.12 解决memory.h中EIGEN处中断问题
猜你喜欢

【五一专属】阿里云ECS大测评#五一专属|向所有热爱分享的“技术劳动者”致敬#

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

Copy攻城狮的年度之“战”|回顾2020

Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers

【Copy攻城狮日志】“一分钟”跑通MindSpore的LeNet模型

The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation

Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience

光条中心提取方法总结(一)

fuser 使用—— YOLOV5内存溢出——kill nvidai-smi 无pid 的 GPU 进程

Copy Siege Lions "sticky" to AI couplets
随机推荐
双向LSTM
【论文阅读】Multi-View Spectral Clustering with Optimal Neighborhood Laplacian Matrix
基于BiGRU和GAN的数据生成方法
JPEG2jpg
PyTorch
深度确定性策略梯度(DDPG)
机器学习——分类问题对于文字标签的处理(特征工程)
DRA821 环境搭建
详解近端策略优化
sbl_init.asm-适合在编辑模式下看
The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
pytorch学习-没掌握的点
MySQL leftmost prefix principle [I understand hh]
TensorFlow: tf.ConfigProto() and Session
TensorFlow2 study notes: 7. Optimizer
【CV-Learning】Convolutional Neural Network
CSDN大礼包--高校圆桌派大礼包
软著撰写注意事项
MNIST手写数字识别 —— 从零构建感知机实现二分类
投稿相关