Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1851: Minimum Interval to Include Each Query

In this guide, we solve Leetcode #1851 Minimum Interval to Include Each Query in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] describes the ith interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Array, Binary Search, Sorting, Line Sweep, Heap (Priority Queue)

Intuition

The problem structure suggests a monotonic decision, which makes binary search a natural fit.

By halving the search space each step, we reach the answer efficiently.

Approach

Search either directly on a sorted array or on the answer space using a check function.

Each check is fast, and the logarithmic search keeps the overall runtime low.

Steps:

  • Define the search bounds.
  • Check the mid point condition.
  • Narrow the bounds until convergence.

Example

Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5] Output: [3,3,1,4] Explanation: The queries are processed as follows: - Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3. - Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3. - Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1. - Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.

Python Solution

class Solution: def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]: n, m = len(intervals), len(queries) intervals.sort() queries = sorted((x, i) for i, x in enumerate(queries)) ans = [-1] * m pq = [] i = 0 for x, j in queries: while i < n and intervals[i][0] <= x: a, b = intervals[i] heappush(pq, (b - a + 1, b)) i += 1 while pq and pq[0][1] < x: heappop(pq) if pq: ans[j] = pq[0][0] return ans

Complexity

The time complexity is O(n×log⁡n+m×log⁡m)O(n \times \log n + m \times \log m)O(n×logn+m×logm), and the space complexity is O(n+m)O(n + m)O(n+m). The space complexity is O(n+m)O(n + m)O(n+m).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy