757. Set Intersection Size At Least Two
An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, including a and b.
Find the minimum size of a set S such that for every integer interval A in intervals, the intersection of S with A has size at least 2.
Example 1:
Input: intervals = [[1, 3], [1, 4], [2, 5], [3, 5]]
Output: 3
Explanation:
Consider the set S = {2, 3, 4}. For each interval, there are at least 2 elements from S in the interval.
Also, there isn't a smaller size set that fulfills the above condition.
Thus, we output the size of this set, which is 3.
Example 2:
Input: intervals = [[1, 2], [2, 3], [2, 4], [4, 5]]
Output: 5
Explanation:
An example of a minimum sized set is {1, 2, 3, 4, 5}.
Note:
intervalswill have length in range[1, 3000].intervals[i]will have length2, representing some integer interval.intervals[i][j]will be an integer in[0, 10^8].
Approach #1: C++. [greedy]
class Solution {
public:
int intersectionSizeTwo(vector<vector<int>>& intervals) {
int size = intervals.size();
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[1] == b[1]) return a[0] > b[0];
else return a[1] < b[1];
});
int ans = 0, p1 = -1, p2 = -1;
for (int i = 0; i < size; ++i) {
if (intervals[i][0] <= p1) continue;
if (intervals[i][0] > p2) {
ans += 2;
p2 = intervals[i][1];
p1 = p2 - 1;
} else {
ans++;
p1 = p2;
p2 = intervals[i][1];
}
}
return ans;
}
};
757. Set Intersection Size At Least Two的更多相关文章
- 【leetcode】757. Set Intersection Size At Least Two
题目如下: 解题思路:贪心算法.首先把intervals按第二个元素从小到大排序,然后遍历intervals,如果集合S和intervals[i]没有交集,那么把intervals[i]的最大值和次大 ...
- [LeetCode] Set Intersection Size At Least Two 设置交集大小至少为2
An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, ...
- [Swift]LeetCode757. 设置交集大小至少为2 | Set Intersection Size At Least Two
An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from ato b, ...
- 跳跃游戏 12 · Jump Game 12
跳跃游戏 1 [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: 由于要用itera ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- Altium Designer之多图纸设计
Altium Designer的多图纸功能感觉比较方便:今天翻了下徐老师<Altium Designer 快速入门>里面关于多图纸设计的介绍,再参考了altium 网站的一些资料,算是摸熟 ...
- 注册驱动时如何调用probe函数 ?
platform_driver_register driver_register bus_add_driver //把驱动放入总线的驱动链表里 ...
- IDA Pro 权威指南学习笔记(五) - IDA 主要的数据显示窗口
在默认配置下,IDA(从 6.1 版开始)会在对新二进制文件的初始加载和分析阶段创建 7 个显示窗口 3 个立即可见的窗口分别为 IDA-View 窗口.函数窗口和消息输出窗口 可以通过 View - ...
- linux之fstab文件详解
/etc/fstab是用来存放文件系统的静态信息的文件.位于/etc/目录下,可以用命令less /etc/fstab 来查看,如果要修改的话,则用命令 vi /etc/fstab 来修改. 当系统启 ...
- 如何发布可用于azure的镜像文件
摘要:本篇文章讲述如何将蝉知,禅道和然之发布azure的镜像. azure是微软提供的云服务平台.并且针对中国用户专门开通了www.windowsazure.cn站点.同时还成了微软开放中国公司,推出 ...
- python实现文件加密
前言: 想实现批量文件加密,可惜批量.展时没有思路 0x1 没有加密前的图片 加密后!!! !!!打不开了 0x02: 代码 import hashlib def get_sha1(f): xd=op ...
- 免Oracle客户端程序监听程序配置
Oracle默认安装时,监听程序和tnsnames程序中的监听方式都是默认的localhost,但免客户端的程序是连接不上的.这时需要: 1.将listener中的(HOST = localhost) ...
- ubuntu14.10下Qt5.4无法输入中文
最近学习Qt,于是在ubuntu下安装了开发环境.我是从官网上下载安装的Qt5.4版本.安装后发现在开发过程中无法输入中文.于是在网上搜了搜.解决办法如下: 1.安装fcitx-frontend-qt ...
- cxf和axis2使用有感
CXF框架 个人不喜欢使用wsimport工具: 1.考虑到远端的服务接口发生变化,本地的接口还需要重新同步下 2.项目中无端多了些冗余的代码 这样我们选择cxf的动态调用接口吧,使用DynamicC ...
- Yii2中ACF和RBAC
ACF ( Access Control Filter) ACF ( Access Control Filter)官网的解释就是一个可以在模型或控制器执行行为过滤器,当有用户请求时,ACF将检查acc ...