返回介绍

solution / 1800-1899 / 1893.Check if All the Integers in a Range Are Covered / README_EN

发布于 2024-06-17 01:03:13 字数 4809 浏览 0 评论 0 收藏 0

1893. Check if All the Integers in a Range Are Covered

中文文档

Description

You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.

Return true _if each integer in the inclusive range_ [left, right] _is covered by at least one interval in_ ranges. Return false _otherwise_.

An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.

 

Example 1:

Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
Output: true
Explanation: Every integer between 2 and 5 is covered:
- 2 is covered by the first range.
- 3 and 4 are covered by the second range.
- 5 is covered by the third range.

Example 2:

Input: ranges = [[1,10],[10,20]], left = 21, right = 21
Output: false
Explanation: 21 is not covered by any range.

 

Constraints:

  • 1 <= ranges.length <= 50
  • 1 <= starti <= endi <= 50
  • 1 <= left <= right <= 50

Solutions

Solution 1

class Solution:
  def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
    diff = [0] * 52
    for l, r in ranges:
      diff[l] += 1
      diff[r + 1] -= 1
    cur = 0
    for i, x in enumerate(diff):
      cur += x
      if left <= i <= right and cur == 0:
        return False
    return True
class Solution {
  public boolean isCovered(int[][] ranges, int left, int right) {
    int[] diff = new int[52];
    for (int[] range : ranges) {
      int l = range[0], r = range[1];
      ++diff[l];
      --diff[r + 1];
    }
    int cur = 0;
    for (int i = 0; i < diff.length; ++i) {
      cur += diff[i];
      if (i >= left && i <= right && cur == 0) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool isCovered(vector<vector<int>>& ranges, int left, int right) {
    int diff[52]{};
    for (auto& range : ranges) {
      int l = range[0], r = range[1];
      ++diff[l];
      --diff[r + 1];
    }
    int cur = 0;
    for (int i = 0; i < 52; ++i) {
      cur += diff[i];
      if (i >= left && i <= right && cur <= 0) {
        return false;
      }
    }
    return true;
  }
};
func isCovered(ranges [][]int, left int, right int) bool {
  diff := [52]int{}
  for _, rg := range ranges {
    l, r := rg[0], rg[1]
    diff[l]++
    diff[r+1]--
  }
  cur := 0
  for i, x := range diff {
    cur += x
    if i >= left && i <= right && cur <= 0 {
      return false
    }
  }
  return true
}
function isCovered(ranges: number[][], left: number, right: number): boolean {
  const diff = new Array(52).fill(0);
  for (const [l, r] of ranges) {
    ++diff[l];
    --diff[r + 1];
  }
  let cur = 0;
  for (let i = 0; i < 52; ++i) {
    cur += diff[i];
    if (i >= left && i <= right && cur <= 0) {
      return false;
    }
  }
  return true;
}
/**
 * @param {number[][]} ranges
 * @param {number} left
 * @param {number} right
 * @return {boolean}
 */
var isCovered = function (ranges, left, right) {
  const diff = new Array(52).fill(0);
  for (const [l, r] of ranges) {
    ++diff[l];
    --diff[r + 1];
  }
  let cur = 0;
  for (let i = 0; i < 52; ++i) {
    cur += diff[i];
    if (i >= left && i <= right && cur <= 0) {
      return false;
    }
  }
  return true;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文