当前位置:网站首页>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]);
}
};
边栏推荐
- Unity ML-agents 参数设置解明
- Use of double pointers
- Code to celebrate the Dragon Boat Festival - Zongzi, your heart
- 审稿意见回复
- MNIST Handwritten Digit Recognition - Image Analysis Method for Binary Classification
- 中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
- Copy攻城狮5分钟在线体验 MindIR 格式模型生成
- TensorFlow: tf.ConfigProto() and Session
- 基于BiGRU和GAN的数据生成方法
- AWS uses EC2 to reduce the training cost of DeepRacer: DeepRacer-for-cloud practical operation
猜你喜欢

arm-2-基础阶段

No matching function for call to 'RCTBridgeModuleNameForClass'

Brief description of database and common operation guide

No matching function for call to ‘RCTBridgeModuleNameForClass‘

在AWS-EC2中安装Minikube集群

arm交叉编译

Copy攻城狮5分钟在线体验 MindIR 格式模型生成

Pytorch语义分割理解

Install Minikube Cluster in AWS-EC2

PyTorch
随机推荐
【五一专属】阿里云ECS大测评#五一专属|向所有热爱分享的“技术劳动者”致敬#
DRA821 环境搭建
TypeError: load() missing 1 required positional argument: ‘Loader‘
MNIST手写数字识别 —— 从零构建感知机实现二分类
Cut the hit pro subtitles export of essays
计算某像素点法线
Comparison of oracle's number and postgresql's numeric
中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
TensorFlow2 study notes: 7. Optimizer
深度确定性策略梯度(DDPG)
Golang environment variable settings (2)--GOMODULE & GOPROXY
MNIST手写数字识别 —— 从感知机到卷积神经网络
PP-LiteSeg
Vision Transformer 论文 + 详解( ViT )
(导航页)OpenStack-M版-双节点手工搭建-附B站视频
How to grow into a senior engineer?
JPEG2jpg
Pytorch问题总结
arm交叉编译
【CV-Learning】Image Classification