Check if All the Integers in a Range Are Covered — LeetCode 1893 Python Solution
EasyArrayHash TablePrefix Sum
- Problem
- #1893
- Pattern
- Prefix Sum
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
- Output
- true
- Explanation
- Every integer between 2 and 5 is covered:
Python solution
Python
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
s = 0
for i, x in enumerate(diff):
s += x
if s <= 0 and left <= i <= right:
return False
return TrueComplexity
| Measure | Complexity |
|---|---|
| Time | O(n + M) |
| Space | O(M) auxiliary |
Pattern: Prefix Sum
Precompute running totals once so any range query becomes a single subtraction. LeetCode 1893. Check if All the Integers in a Range Are Covered is filed here because LeetCode tags it Prefix Sum, which is the vocabulary this hub collects.
The prefix sum guide has the Python template for the pattern and the 157 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1893. Check if All the Integers in a Range Are Covered?
- LeetCode 1893. Check if All the Integers in a Range Are Covered is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1893. Check if All the Integers in a Range Are Covered?
- The Python solution on this page runs in O(n + M).
- What is the space complexity of LeetCode 1893. Check if All the Integers in a Range Are Covered?
- The Python solution on this page uses O(M) auxiliary space.
- What topics does LeetCode 1893. Check if All the Integers in a Range Are Covered cover?
- LeetCode 1893. Check if All the Integers in a Range Are Covered is tagged Array, Hash Table and Prefix Sum on LeetCode.