Minimum Interval to Include Each Query
Detailed guide and Python implementation for the 'Minimum Interval to Include Each Query' problem.
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.
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.