1893:检查是否区域内所有整数都被覆盖
This commit is contained in:
parent
90854f34fe
commit
591223b339
@ -0,0 +1,80 @@
|
||||
//给你一个二维整数数组 ranges 和两个整数 left 和 right 。每个 ranges[i] = [starti, endi] 表示一个从 star
|
||||
//ti 到 endi 的 闭区间 。
|
||||
//
|
||||
// 如果闭区间 [left, right] 内每个整数都被 ranges 中 至少一个 区间覆盖,那么请你返回 true ,否则返回 false 。
|
||||
//
|
||||
// 已知区间 ranges[i] = [starti, endi] ,如果整数 x 满足 starti <= x <= endi ,那么我们称整数x 被覆盖了
|
||||
//。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
|
||||
//输出:true
|
||||
//解释:2 到 5 的每个整数都被覆盖了:
|
||||
//- 2 被第一个区间覆盖。
|
||||
//- 3 和 4 被第二个区间覆盖。
|
||||
//- 5 被第三个区间覆盖。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:ranges = [[1,10],[10,20]], left = 21, right = 21
|
||||
//输出:false
|
||||
//解释:21 没有被任何一个区间覆盖。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= ranges.length <= 50
|
||||
// 1 <= starti <= endi <= 50
|
||||
// 1 <= left <= right <= 50
|
||||
//
|
||||
// Related Topics 数组 哈希表 前缀和
|
||||
// 👍 52 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
//1893:检查是否区域内所有整数都被覆盖
|
||||
public class CheckIfAllTheIntegersInARangeAreCovered {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CheckIfAllTheIntegersInARangeAreCovered().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isCovered(int[][] ranges, int left, int right) {
|
||||
Arrays.sort(ranges, new Comparator<int[]>() {
|
||||
@Override
|
||||
public int compare(int[] o1, int[] o2) {
|
||||
return o1[0] - o2[0];
|
||||
}
|
||||
});
|
||||
for (int[] arr : ranges) {
|
||||
if (arr[0] > left) {
|
||||
return false;
|
||||
}
|
||||
if (arr[1] >= right) {
|
||||
return true;
|
||||
}
|
||||
if (arr[1] >= left) {
|
||||
left = arr[1] + 1;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<p>给你一个二维整数数组 <code>ranges</code> 和两个整数 <code>left</code> 和 <code>right</code> 。每个 <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 表示一个从 <code>start<sub>i</sub></code> 到 <code>end<sub>i</sub></code> 的 <strong>闭区间</strong> 。</p>
|
||||
|
||||
<p>如果闭区间 <code>[left, right]</code> 内每个整数都被 <code>ranges</code> 中 <strong>至少一个</strong> 区间覆盖,那么请你返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p>已知区间 <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ,如果整数 <code>x</code> 满足 <code>start<sub>i</sub> <= x <= end<sub>i</sub></code> ,那么我们称整数<code>x</code> 被覆盖了。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>2 到 5 的每个整数都被覆盖了:
|
||||
- 2 被第一个区间覆盖。
|
||||
- 3 和 4 被第二个区间覆盖。
|
||||
- 5 被第三个区间覆盖。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>ranges = [[1,10],[10,20]], left = 21, right = 21
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>21 没有被任何一个区间覆盖。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranges.length <= 50</code></li>
|
||||
<li><code>1 <= start<sub>i</sub> <= end<sub>i</sub> <= 50</code></li>
|
||||
<li><code>1 <= left <= right <= 50</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>前缀和</li></div></div>\n<div><li>👍 52</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user