Minimum Interval to Include Each Query
Detailed guide and Python implementation for the 'Minimum Interval to Include Each Query' problem.
1. Concept Overview
The 'Minimum Interval to Include Each Query' problem is a key challenge in the Intervals section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Minimum Interval to Include Each Query.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Minimum Interval to Include Each Query carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
You are given a 2D integer array intervals, where intervals[i] = [left_i, right_i] describes the ith interval starting at left_i and ending at right_i (inclusive). The size of an interval is defined as right_i - left_i + 1. You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such that left_i <= queries[j] <= right_i. If no such interval exists, the answer is -1.
Return an array containing the answers to the queries.
Write a function minInterval(intervals: List[List[int]], queries: List[int]) -> List[int].
- •1 <= len(intervals) <= 10^5
- •1 <= len(queries) <= 10^5
- •intervals[i].length == 2
- •1 <= left_i <= right_i <= 10^7
- •1 <= queries[j] <= 10^7
Examples
intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
[3,3,1,4]
Smallest interval containing 2 is [2,4] (size 3). For 3 is [2,4] (size 3). For 4 is [4,4] (size 1). For 5 is [3,6] (size 4).
intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
[2,-1,4,6]
For 2: [2,3] (size 2). For 19: none (-1). For 5: [2,5] (size 4). For 22: [20,25] (size 6).
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
import heapq
def min_interval_opt(intervals, queries):
intervals.sort()
minHeap = []; res, i = {}, 0
for q in sorted(queries):
while i < len(intervals) and intervals[i][0] <= q:
l, r = intervals[i]
heapq.heappush(minHeap, (r - l + 1, r))
i += 1
while minHeap and minHeap[0][1] < q: heapq.heappop(minHeap)
res[q] = minHeap[0][0] if minHeap else -1
return [res[q] for q in queries]Brute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def min_interval_brute(intervals, queries):
res = []
for q in queries:
min_len = float("inf")
for i in intervals:
if i[0] <= q <= i[1]:
min_len = min(min_len, i[1] - i[0] + 1)
res.append(min_len if min_len != float("inf") else -1)
return resAlgorithm Pattern Checklist
When dealing with Intervals data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Generators
Learn how to use Python generators and yield statements to process huge datasets with minimal memory footprints. Master generator expressions.
How to Convert String to Int in Python
Learn how to convert a string to an integer in Python using the int() function. Handle errors safely and convert numbers from binary, octal, or hex.
Python Operators
Master arithmetic, comparison, logical, bitwise, assignment, and identity operators in Python.
Python Decorators vs Decorator Design Pattern: The Key Differences
Compare Python decorators and the classic decorator design pattern. Understand the differences between definition-time function wrapping and runtime dynamic object composition with runnable code.