forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize_ranges.py
More file actions
44 lines (35 loc) · 1.05 KB
/
summarize_ranges.py
File metadata and controls
44 lines (35 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Summarize Ranges
Given a sorted integer array without duplicates, return the summary of its
ranges as a list of (start, end) tuples.
Reference: https://leetcode.com/problems/summary-ranges/
Complexity:
Time: O(n)
Space: O(n) for the result list
"""
from __future__ import annotations
def summarize_ranges(array: list[int]) -> list[tuple[int, ...]]:
"""Summarize consecutive runs in a sorted array as (start, end) tuples.
Args:
array: Sorted list of unique integers.
Returns:
List of (start, end) tuples for each consecutive range.
Examples:
>>> summarize_ranges([0, 1, 2, 4, 5, 7])
[(0, 2), (4, 5), (7, 7)]
"""
result = []
if len(array) == 0:
return []
if len(array) == 1:
return [(array[0], array[0])]
iterator = iter(array)
start = end = next(iterator)
for num in iterator:
if num - end == 1:
end = num
else:
result.append((start, end))
start = end = num
result.append((start, end))
return result