当前位置:网站首页>The 25th day of the special assault version of the sword offer
The 25th day of the special assault version of the sword offer
2022-08-10 03:20:00 【_hys】
排序+贪心
- Sort the interval by the left endpoint in ascending order,Then there is a property:The left endpoint of the next interval<=当前右端点<=当前左端点.
- It is easy to think that it is impossible to merge if the current right endpoint is smaller than the left endpoint of the next interval,If it is greater than or equal to, then take the maximum of the current right endpoint and the next right endpoint.
class Solution {
public:
bool cmp(vector<int> &a, vector<int> &b) {
if(a[0] != b[0]) return a[0] < b[0];
return a[1] < b[1];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
int st = intervals[0][0], ed = intervals[0][1];
vector<vector<int>> res;
for(int i = 1; i < intervals.size(); i++) {
const auto seg = intervals[i];
if(ed < seg[0]) {
res.emplace_back(vector<int>{
st,ed});
st = seg[0];
ed = seg[1];
} else {
ed = max(ed,seg[1]);
}
}
if(res.empty() || ed != res.back()[1]) res.emplace_back(vector<int>{
st,ed});
return res;
}
};
计数排序
发现 a r r 1 arr1 arr1 range of data [ 0 , 1000 ] [0,1000] [0,1000] It can be directly counted and sorted, O ( N ) O(N) O(N) 的时间复杂度.
注意:After you finish inserting the front part,whileconditions will appear-1,需要加上>0条件.
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
int cnts[1010] = {
0};
vector<int> res;
for(auto num: arr1) cnts[num]++;
for(auto num: arr2)
while(cnts[num]--) res.emplace_back(num);
for(int i = 0; i <= 1000; i++)
while(cnts[i]-- > 0)
res.emplace_back(i);
return res;
}
};
边栏推荐
猜你喜欢

【二叉树-中等】687. 最长同值路径

T5: Text-to-Text Transfer Transformer

FusionCompute产品介绍

【Kali安全渗透测试实践教程】第6章 密码攻击

Pagoda server PHP+mysql web page URL jump problem

Introduction and application of quantitative trading strategies

实操|风控模型中常用的这三种预测方法与多分类场景的实现

【二叉树-中等】1379. 找出克隆二叉树中的相同节点

RESOURCE_EXHAUSTED: etcdserver: mvcc: database space exceeded

Open3D 泊松盘网格采样
随机推荐
进程管理和任务管理
Pagoda server PHP+mysql web page URL jump problem
[语法糖] 关于类别字符串到类别数字id的映射
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
数据在内存中的存储
量化交易策略介绍及应用市值中性化选股
数据挖掘和数据仓库之间的区别
openpose脚部标注问题梳理
.Net面试经验总结
2022.8.9考试游记总结
storage of data in memory
grafana9配置邮箱告警
ECCV 2022 Oral | CCPL: 一种通用的关联性保留损失函数实现通用风格迁移
【二叉树-中等】2265. 统计值等于子树平均值的节点数
2022.8.9考试立方和--1100题解
2022年8月8日-2022年8月15日,ue4视频教程+插件源码()
liunx PS1 设置
《GB39707-2020》PDF下载
【8.8】代码源 - 【不降子数组游戏】【最长上升子序列计数(Bonus)】【子串(数据加强版)】
将信号与不同开始时间对齐