↓Skip to main content
  1. LeetCode/

LeetCode 228: Summary Ranges

·2 mins· ·
LeetCode Easy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-21

  • Submissions: Runtime Error ×3 → Wrong Answer ×4 → Runtime Error ×2 → Accepted ×4 → Runtime Error → Accepted

Approach
#

Keep a list of ranges: a number x equal to the last range’s end plus one extends it, otherwise open [x, x]. Finally format a == b as "a" and others as "a->b". The last range and the empty array need no special handling.

My Attempts
#

This took 15 submissions:

  • The first versions emitted a range only when the next number jumped by more than 1, so the final range was never emitted, and an empty array returned None: [3] returned [].
  • Handling the last element by index then kept reading nums[i + 1] out of range.
  • The first accepted version was full of special cases; the final rewrite, “extend the last range when the number continues it”, removed every boundary problem.

Solution
#

class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        ranges = []
        for x in nums:
            if ranges and ranges[-1][1] == x - 1:
                ranges[-1][1] = x
            else:
                ranges.append([x, x])
        return [str(a) if a == b else f"{a}->{b}" for a, b in ranges]

Complexity
#

Time O(n), space O(n) (output).

Mistakes to Avoid
#

  • Emit the final range.
  • An empty array returns an empty list, not None.
  • A single-number range is written "a".

Interview Explanation
#

In one pass I keep a list of ranges, extending the last one when a number continues it and opening a new one otherwise, then format them. Time O(n).

Related

LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate
LeetCode 219: Contains Duplicate II
·2 mins
LeetCode Easy Array Hash-Table Sliding-Window
LeetCode 219 note: a hash map stores each value’s last index; only the most recent occurrence needs a distance check
LeetCode 169: Majority Element
·2 mins
LeetCode Easy Array Hash-Table Divide-and-Conquer Sorting
LeetCode 169 note: Boyer-Moore voting, where different values cancel in pairs and the survivor is the one appearing more than half the time
LeetCode 108: Convert Sorted Array to Binary Search Tree
·1 min
LeetCode Easy Array Divide-and-Conquer Tree Binary-Search-Tree
LeetCode 108 note: the middle element becomes the root and each half recurses; the halves differ by at most one, so the tree is balanced
LeetCode 119: Pascal's Triangle II
·2 mins
LeetCode Easy Array Dynamic-Programming
LeetCode 119 note: only row rowIndex is needed; update a 1D array from right to left for O(k) space
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written