当前位置:网站首页>leetcode-设置交集大小至少为2
leetcode-设置交集大小至少为2
2022-07-23 09:35:00 【程序猿不脱发2】
题目:
一个整数区间 [a, b] ( a < b ) 代表着从 a 到 b 的所有连续整数,包括 a 和 b。
给你一组整数区间intervals,请找到一个最小的集合 S,使得 S 里的元素与区间intervals中的每一个整数区间都至少有2个元素相交。
输出这个最小集合S的大小。
示例 1:
输入: intervals = [[1, 3], [1, 4], [2, 5], [3, 5]]
输出: 3
解释:
考虑集合 S = {2, 3, 4}. S与intervals中的四个区间都有至少2个相交的元素。
且这是S最小的情况,故我们输出3。
示例 2:
输入: intervals = [[1, 2], [2, 3], [2, 4], [4, 5]]
输出: 5
解释:
最小的集合S = {1, 2, 3, 4, 5}.
注意:
intervals 的长度范围为[1, 3000]。
intervals[i] 长度为 2,分别代表左、右边界。
intervals[i][j] 的值是 [0, 10^8]范围内的整数。
java代码:
class Solution {
public int intersectionSizeTwo(int[][] intervals) {
int n = intervals.length;
int res = 0;
int m = 2;
Arrays.sort(intervals, (a, b) -> {
if (a[0] == b[0]) {
return b[1] - a[1];
}
return a[0] - b[0];
});
List<Integer>[] temp = new List[n];
for (int i = 0; i < n; i++) {
temp[i] = new ArrayList<Integer>();
}
for (int i = n - 1; i >= 0; i--) {
for (int j = intervals[i][0], k = temp[i].size(); k < m; j++, k++) {
res++;
help(intervals, temp, i - 1, j);
}
}
return res;
}
public void help(int[][] intervals, List<Integer>[] temp, int pos, int num) {
for (int i = pos; i >= 0; i--) {
if (intervals[i][1] < num) {
break;
}
temp[i].add(num);
}
}
}
边栏推荐
- mysql唯一索引无重复值报错重复
- Leetcode-227-basic calculator||
- 452. 用最少数量的箭引爆气球
- Official wechat product! Applet automation framework minium sharing Preview
- Is it risky and safe to open an account for stock speculation?
- 104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
- [WinForm] desktop program implementation scheme for screenshot recognition and calculation
- Canvas from getting started to persuading friends to give up (graphic version)
- [untitled]
- 中望CAD专业版 2022软件安装包下载及安装教程
猜你喜欢
It is suggested that Siyuan notes can be compatible with third-party sync disks

利用js自动解析执行xss

Qu'est - ce que le codage par titre?

mysql函数汇总之字符串函数

运维高级作业03

Wacom firmware update error 123, digital board driver cannot be updated

中望CAD专业版 2022软件安装包下载及安装教程

AI acceleration gesture recognition experience based on efr32mg24

Authing supports Zadig! Unified authentication and rapid docking of cloud native users

Detailed tutorial of typora drawing bed configuration
随机推荐
【C語言】猜數字小遊戲+關機小程序
【数组&&字符串&&宏练习题】
Official wechat product! Applet automation framework minium sharing Preview
linux定时备份数据库脚本
Palindrome related topics
Qt文档阅读笔记-Audio Example解析
Can bus quick understanding
Zhongwang CAD professional 2022 software installation package download and installation tutorial
LeetCode-227-基本计算器||
Oracle 报表常用sql
如何实现多个传感器与西门子PLC之间485无线通讯?
[untitled]
Canvas from getting started to persuading friends to give up (graphic version)
【小程序自动化Minium】一、框架介绍和环境搭建
Towhee weekly model
Towhee weekly model
Regular expression common syntax parsing
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
Which is a good fixed asset management system? What are the fixed asset management platforms?
After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting