Leetcode 1893: Check if All the Integers in a Range Are Covered

grid47
grid47
Exploring patterns and algorithms
May 1, 2024 5 min read

You are given a list of integer ranges and two integers, left and right. Each range is represented by a pair of integers [start, end]. You need to determine if all integers from left to right (inclusive) are covered by at least one of these ranges. An integer x is considered covered by a range [start, end] if start <= x <= end.
Problem
Approach
Steps
Complexity
Input: The input consists of a 2D integer array 'ranges' and two integers, left and right.
Example: For ranges = [[1,3], [5,6], [2,4]], left = 2, right = 5
Constraints:
• 1 <= ranges.length <= 50
• 1 <= start <= end <= 50
• 1 <= left <= right <= 50
Output: Return true if every integer between left and right (inclusive) is covered by at least one of the given ranges. Otherwise, return false.
Example: For ranges = [[1,3], [5,6], [2,4]], left = 2, right = 5, the output will be true.
Constraints:
• The output is a boolean value: true or false.
Goal: The goal is to check if every integer from left to right is covered by at least one range.
Steps:
• Create an array to track coverage of each integer from 1 to 50.
• Iterate through each range and mark the covered integers.
• Check if each integer in the range [left, right] is covered by at least one range.
Goal: The input is constrained to be within certain limits.
Steps:
• The ranges array will have at most 50 elements.
• Each element of the ranges will contain two integers between 1 and 50.
Assumptions:
• The input ranges array will contain valid integer intervals.
• The values of left and right will be within the bounds of the range.
Input: ranges = [[1,3], [5,6], [2,4]], left = 2, right = 5
Explanation: Each integer between 2 and 5 is covered by at least one range: 2 and 3 are covered by the first range, 5 is covered by the second range, and 4 is covered by the first range.

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

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

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus