LeetCode 1893: Check if All the Integers in a Range Are Covered Solution
Master LeetCode problem 1893 (Check if All the Integers in a Range Are Covered), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1893. Check if All the Integers in a Range Are Covered
Problem Explanation
Explanation
To solve this problem, we can iterate through each integer in the range [left, right] and check if it is covered by at least one interval in the ranges array. We can do this by creating a boolean array of size 51 (to represent integers from 0 to 50) and marking each integer that is covered by an interval. Finally, we check if all integers in the range [left, right] are marked as covered.
- Initialize a boolean array
coveredof size 51, all initialized to false. - Iterate over each interval in the
rangesarray and mark the integers in the range as covered. - Iterate over the range
[left, right]and check if all integers are covered. - Return true if all integers are covered, false otherwise.
Time Complexity: O(n + m), where n is the number of intervals in ranges and m is the size of the range [left, right].
Space Complexity: O(1) since the boolean array covered is of constant size.
Solution Code
class Solution {
public boolean isCovered(int[][] ranges, int left, int right) {
boolean[] covered = new boolean[51]; // Represents integers from 0 to 50
for (int[] range : ranges) {
for (int i = range[0]; i <= range[1]; i++) {
covered[i] = true;
}
}
for (int i = left; i <= right; i++) {
if (!covered[i]) {
return false;
}
}
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1893 (Check if All the Integers in a Range Are Covered)?
This page provides optimized solutions for LeetCode problem 1893 (Check if All the Integers in a Range Are Covered) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1893 (Check if All the Integers in a Range Are Covered)?
The time complexity for LeetCode 1893 (Check if All the Integers in a Range Are Covered) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1893 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1893 in Java, C++, or Python.