Easy

Quiz

#228 Summary Ranges

APPROACH

You are given a sorted array of unique integers nums.

A range [a,b] covers every integer from a to b inclusive.

Return the minimal sorted list of ranges that together cover exactly every element of nums. Each element belongs to exactly one range and no extra integers are included.

Format each range [a,b] as:

- "a->b" when a != b

- "a" when a == b

Example 1:

Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"

Example 2:

Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
1 of 4
1:00

What is the optimal approach for this problem?