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

Leetcode #2655: Find Maximal Uncovered Ranges

In this guide, we solve Leetcode #2655 Find Maximal Uncovered Ranges 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 an integer n which is the length of a 0-indexed array nums, and a 0-indexed 2D-array ranges, which is a list of sub-ranges of nums (sub-ranges may overlap). Each row ranges[i] has exactly 2 cells: ranges[i][0], which shows the start of the ith range (inclusive) ranges[i][1], which shows the end of the ith range (inclusive) These ranges cover some cells of nums and leave some cells uncovered.

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Array, Sorting

Intuition

Sorting reveals structure that is hard to see in the original order.

Once sorted, a linear scan is usually enough to compute the answer.

Approach

Sort the data and sweep through it while maintaining a small state.

This keeps the logic straightforward and reliable.

Steps:

  • Sort the data.
  • Scan in order while maintaining state.
  • Update the best answer.

Example

Input: n = 10, ranges = [[3,5],[7,8]] Output: [[0,2],[6,6],[9,9]] Explanation: The ranges (3, 5) and (7, 8) are covered, so if we simplify the array nums to a binary array where 0 shows an uncovered cell and 1 shows a covered cell, the array becomes [0,0,0,1,1,1,0,1,1,0] in which we can observe that the ranges (0, 2), (6, 6) and (9, 9) aren't covered.

Python Solution

class Solution: def findMaximalUncoveredRanges( self, n: int, ranges: List[List[int]] ) -> List[List[int]]: ranges.sort() last = -1 ans = [] for l, r in ranges: if last + 1 < l: ans.append([last + 1, l - 1]) last = max(last, r) if last + 1 < n: ans.append([last + 1, n - 1]) return ans

Complexity

The time complexity is O(n×log⁡n)O(n \times \log n)O(n×logn), and the space complexity is O(log⁡n)O(\log n)O(logn), where nnn is the length of the array ranges\textit{ranges}ranges. The space complexity is O(log⁡n)O(\log n)O(logn), where nnn is the length of the array ranges\textit{ranges}ranges.

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