carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode1893: Check if All the Integers in a Range Are Covered #302

Open carloscn opened 1 year ago

carloscn commented 1 year ago

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:

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

carloscn commented 1 year ago

Analysis

pub fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool
{
    if ranges.len() < 1 {
        return false;
    }

    let mut ret:bool = false;

    for e in &ranges {
        if left > e[1] || left > e[0] {
            continue;
        } else {
            ret = true;
            break;
        }
    }

    if ret == false {
        return false;
    }

    ret = false;
    for e in &ranges {
        if right > e[1] || right > e[0] {
            continue;
        } else {
            ret = true;
            break;
        }
    }

    return ret;
}
carloscn commented 1 year ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/557594 https://github.com/carloscn/structstudy/commit/489b6bde3be1c6218efb2d03211a8b3fe76284a2