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:
intervals
will 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的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- 什么是Ajax和JSON,它们的优缺点
什么是Ajax??? 术语Ajax用来描述一组技术,它使浏览器可以为用户提供更为自然的浏览体验. Ajax它是“Asynchronous JavaScript + XML的简写” 定义Ajax: Aj ...
- java - 读取,导出 excel文件数据
首先需下载poi java包,添加至构建路径, 写处理方法: import java.io.FileInputStream;import java.io.FileOutputStream;import ...
- hardentools
Hardentools是一组简单的实用程序,旨在禁用操作系统(Microsoft Windows,现在)以及主要的消费者应用程序公开的许多“功能”.这些通常为企业客户所设想的功能,对于普通用户来说通 ...
- Python Twisted系列教程15:测试诗歌
作者:dave@http://krondo.com/tested-poetry/ 译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted 入门!“浏览 ...
- python并发之multiprocessing
由于GIL(全局解释锁)的问题,python多线程并不能充分利用多核处理器.如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.multiprocessing可以给每个进程赋 ...
- android 侧滑菜单
就是用手一滑才出现,占手机半个多屏幕的菜单.为了美观和页面转跳,很多时候要用到. 实现的话就是使用官方的DrawerLayout,注意这个布局一定要是最顶层的布局. 在DrawerLayout里面直接 ...
- 【NOI2002】荒岛野人
[题解] 可以枚举m 那么任意两个野人之间有 c[i]+x*p[i]=c[j]+x*p[j] (mod m) 无解,或 x 的最小值<=min(l[i] , l[j]) 化为丢番图方程:(p[ ...
- 八数码问题——A*大法好
[描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示. 空格周围的棋子可以移到空格中. 要求解的问题是:给出一种初始布局(初始状态)和目标布局( ...
- axure可用密钥
axure8.0注册码 激活码:(亲测可用) 用户名:aaa 注册码:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajxG ...
- 客户端级别的渲染分析工具 dynaTrace
dynaTrace Ajax Edition是一款很好的javaScript性能分析工具.目前支持IE和Firefox 2款浏览器. dynaTrace如图所示: 点击Click here to st ...