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的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- 红黑树(Red-Black Tree)
概念解析: 红黑树是一种自平衡二叉查找树(self-balancing binary search tree).因此,红黑树本身就是二叉树的一个变种.典型的用途是实现关联数组(Associative ...
- Oracle10g客户端连接远程数据库配置图解
yuanwen:http://blog.csdn.net/DKZhu/article/details/6027933 一. 安装oracle客户端 1. 运行setup.exe,出现 2. ...
- web deploy 部署网站
一.服务端配置 1. 确保在服务器端(我目前是win server 2012 R2)安装管理服务 安装后服务器会重启, 2)安装webdeploy http://www.iis.net/downloa ...
- sql中left join on where区别剖析
select from tb1 left join tb2 on tb1.xx=tb2.xx and tb2.xxxx=5 先筛选tb2.xxxx=5 再把tb1与筛选后的临时表进行左连接. sele ...
- Android控件使用自定义字体
我们不可能只满足于系统自带的字体(太丑),其实控件自定义字体也很简单.. 1.首先找到该字体的ttf文件. 2.把字体文件放在scr/mian/assets/fonts下,如果没有该路径则自己创建. ...
- leetcode609
public class Solution { public IList<IList<string>> FindDuplicate(string[] paths) { Dict ...
- EOFException异常的处理
TOmcat启动后报:IOException while loading persisted sessions: Java.io.EOFException错误 - IOException while ...
- 21-从零玩转JavaWeb-多态详解
配套视频详解 多态思想 eclipse快捷键设置 多态的好处 多态方法调用 instanceof关键字 多态中字段注意点 一.什么是多态 既然子类是一种特殊的父类 那么我们可不可以认为 狗 ...
- DAY.15_Python
昨天完成了三级菜单和购物车程序的作业: """ .__author__.=,"JerseyHg" 作业要求:1. 可依次选择进入到下一级菜单:2. 可 ...
- 【原创】7. MYSQL++中的查询结果获取(各种Result类型)
在本节中,我将首先介绍MYSQL++中的查询的几个简单例子用法,然后看一下mysqlpp::Query中的几个与查询相关的方法原型(重点关注返回值),最后对几个关键类型进行解释. 1. MYSQL++ ...