Before attempting this problem, you should be comfortable with:
The simplest approach is to store all incoming numbers in a list and compute the intervals on demand. When getIntervals() is called, we sort the list and scan through it to identify consecutive sequences. Two numbers belong to the same interval if they differ by exactly 1.
addNum(value), append the value to the list.getIntervals():start with the first element.1, close the current interval and start a new one.class SummaryRanges:
def __init__(self):
self.arr = []
def addNum(self, value: int) -> None:
self.arr.append(value)
def getIntervals(self) -> List[List[int]]:
if not self.arr:
return []
self.arr.sort()
n = len(self.arr)
start = self.arr[0]
res = []
for i in range(1, n):
if self.arr[i] - self.arr[i - 1] > 1:
res.append([start, self.arr[i - 1]])
start = self.arr[i]
res.append([start, self.arr[n - 1]])
return resThe brute force approach stores duplicates, which wastes space and processing time during sorting. By using a hash set instead of a list, we automatically eliminate duplicates. This makes the interval computation cleaner since we only deal with unique values.
addNum(value), add the value to the set (duplicates are ignored).getIntervals():start with the first element.1, close the current interval and start a new one.class SummaryRanges:
def __init__(self):
self.arr = set()
def addNum(self, value: int) -> None:
self.arr.add(value)
def getIntervals(self) -> List[List[int]]:
if not self.arr:
return []
lst = sorted(list(self.arr))
n = len(lst)
start = lst[0]
res = []
for i in range(1, n):
if lst[i] - lst[i - 1] > 1:
res.append([start, lst[i - 1]])
start = lst[i]
res.append([start, lst[n - 1]])
return resUsing an ordered map (like TreeMap or SortedDict), we can maintain elements in sorted order as they are inserted. This eliminates the need to sort during getIntervals(). We simply iterate through the keys in order and merge consecutive numbers into intervals on the fly.
addNum(value), insert the value into the map.getIntervals():[n, n].from sortedcontainers import SortedDict
class SummaryRanges:
def __init__(self):
self.treeMap = SortedDict()
def addNum(self, value: int) -> None:
self.treeMap[value] = True
def getIntervals(self) -> List[List[int]]:
res = []
for n in self.treeMap:
if res and res[-1][1] + 1 == n:
res[-1][1] = n
else:
res.append([n, n])
return resAn ordered set provides the same benefits as an ordered map but with simpler semantics when we only need to track the presence of numbers. Elements are kept sorted automatically, and duplicates are ignored. The interval construction logic remains the same: iterate in order and merge consecutive numbers.
addNum(value), insert the value into the set.getIntervals():[n, n].from sortedcontainers import SortedSet
class SummaryRanges:
def __init__(self):
self.orderedSet = SortedSet()
def addNum(self, value: int) -> None:
self.orderedSet.add(value)
def getIntervals(self) -> List[List[int]]:
res = []
for n in self.orderedSet:
if res and res[-1][1] + 1 == n:
res[-1][1] = n
else:
res.append([n, n])
return resWhen adding numbers to the data stream, duplicates may appear. If using a list-based approach, duplicates cause intervals like [1, 1] to appear multiple times or create incorrect interval boundaries. Using a set automatically handles this.
# Wrong: allows duplicates
self.arr.append(value)
# Correct: prevents duplicates
self.arr.add(value)When merging intervals, two numbers are consecutive if they differ by exactly 1. A common mistake is using >= instead of > when checking for gaps, which incorrectly merges non-consecutive numbers.
# Wrong: merges [1,2] and [2,3] into one interval incorrectly
if arr[i] - arr[i - 1] >= 1:
# start new interval
# Correct: only starts new interval when gap > 1
if arr[i] - arr[i - 1] > 1:
res.append([start, arr[i - 1]])
start = arr[i]When iterating through sorted numbers to build intervals, the loop only closes intervals when a gap is found. The last interval is never closed inside the loop and must be added after.
# Wrong: missing final interval
for i in range(1, n):
if lst[i] - lst[i - 1] > 1:
res.append([start, lst[i - 1]])
start = lst[i]
return res # Missing the last interval!
# Correct: add final interval after loop
res.append([start, lst[n - 1]])
return resWhen using ordered structures and extending intervals on the fly, the condition to extend must check if the current number is exactly one more than the interval end, not just greater.
# Wrong: extends interval incorrectly
if res and res[-1][1] < n:
res[-1][1] = n
# Correct: only extend if consecutive
if res and res[-1][1] + 1 == n:
res[-1][1] = n
else:
res.append([n, n])When getIntervals() is called before any numbers are added, or when all numbers have been processed, the function should return an empty list. Forgetting this check causes index errors.
# Wrong: crashes on empty input
start = self.arr[0] # IndexError if empty
# Correct: handle empty case first
if not self.arr:
return []
start = self.arr[0]