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

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

Rust Solution

struct Solution;

use std::collections::HashSet;

impl Solution {
    fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool {
        let mut a = HashSet::new();
        let mut b = HashSet::new();
        for i in left..=right {
            b.insert(i);
        }
        for r in ranges {
            let start = r[0];
            let end = r[1];
            for i in start..=end {
                if b.contains(&i) {
                    a.insert(i);
                }
            }
        }
        a == b
    }
}

#[test]
fn test() {
    let ranges = vec_vec_i32![[1, 2], [3, 4], [5, 6]];
    let left = 2;
    let right = 5;
    let res = true;
    assert_eq!(Solution::is_covered(ranges, left, right), res);
    let ranges = vec_vec_i32![[1, 10], [10, 20]];
    let left = 21;
    let right = 21;
    let res = false;
    assert_eq!(Solution::is_covered(ranges, left, right), res);
}

Having problems with this solution? Click here to submit an issue on github.