当前位置:网站首页>Leetcode 757 set the intersection size to at least 2[sort greedy] the leetcode path of heroding
Leetcode 757 set the intersection size to at least 2[sort greedy] the leetcode path of heroding
2022-07-23 07:36:00 【HERODING23】

Their thinking :
A relatively complex greedy problem , Because at least two intersecting elements , Therefore, we need to define an array for each interval to store intersecting intervals to reduce the number of repetitions and ensure the minimum output . First, sort the array according to the beginning of the interval , Then traverse the array from back to front , For each interval , Choose several numbers according to the number that coincides with the interval of thick surface ( Of course not more than 2), If an interval coincides , Just choose another one , Overlap the two intervals and skip directly , The code is as follows :
class Solution {
public:
void help(vector<vector<int>>& intervals, vector<vector<int>>& temp, int pos, int num) {
for (int i = pos; i >= 0; i--) {
if (intervals[i][1] < num) {
break;
}
temp[i].push_back(num);
}
}
int intersectionSizeTwo(vector<vector<int>>& intervals) {
int n = intervals.size();
int res = 0;
sort(intervals.begin(), intervals.end(), [&](vector<int>& a, vector<int>& b) {
if (a[0] == b[0]) {
return a[1] > b[1];
}
return a[0] < b[0];
});
vector<vector<int>> temp(n);
for (int i = n - 1; i >= 0; i--) {
for (int j = intervals[i][0], k = temp[i].size(); k < 2; j++, k++) {
res++;
help(intervals, temp, i - 1, j);
}
}
return res;
}
};
边栏推荐
- RS485 communication OSI model network layer
- 【无标题】分享一个基于Abp Vnext开发的API网关项目
- 分析伦敦银的实时行行发展方法
- 7、学习MySQL 选择数据库
- 小程序毕设作品之微信校园二手书交易小程序毕业设计成品(2)小程序功能
- Custom view: levitation ball and accelerator ball
- Redis——JedisConnectionException Could not get a resource from the pool
- 使用Flutter与贝塞尔曲线画一个波浪球
- 初识Flutter中的Layer
- [C# 数组]-C# 之中的一维数组和对象数组
猜你喜欢
随机推荐
Unity 笔记——Addressables的使用
STM32CubeIDE链接脚本讲解
Persistence of redis
企业生产线改善毕业论文【Flexsim仿真实例】
tensorflow2.0稀疏矩阵输入操作
UNO/ESP8266 for TCA9548A模块双通道驱动2块SH1106 1.3“显示
Clever use of curl
excel 将图片的链接URL 显示为图片 转
GNU LD script command language (II)
初识Flutter中的Layer
聪明人的游戏提高篇:第三章第三课例题:素数的秘密(prime)
你的 NFT 会消失吗?DFINITY 提供 NFT 存储最佳方案
GNU LD脚本命令语言(一)
IP第二次实验 MGRE OSPF
Wechat hotel reservation applet graduation project (5) assignment
使用Flutter与贝塞尔曲线画一个波浪球
基于softmax激活变换的对抗防御方法
UE4引擎的CopyTexture, CopyToResolveTarget
小程序毕设作品之微信酒店预订小程序毕业设计(7)中期检查报告
ES6解决异步问题




![[technology popularization] alliance chain layer2- on a new possibility](/img/e1/be9779eee3d3d4dcf56e103ba1d3d6.jpg)



