Leet Code 101: How to solve the “Summary Ranges” problem

Leet Code 101: How to solve the “Summary Ranges” problem

Summary ranges is an easy problem. You need to understand “sliding window” concept to solve it properly.

Problem

You are given a sorted unique integer array .

A range is the set of all integers from to (inclusive).

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of is covered by exactly one of the ranges, and there is no integer such that is in one of the ranges but not in .

Each range in the list should be output as:

if

if

Let’s understand by using an example:

Now, we have to convert nums into ranges, let’s start by creating the first range, from 0–2, since 3 is not covered, we can’t add 3 to our range:

Let’s continue by adding the other ranges, 4–5 and 7

Summary Ranges

We will just use the same algorithm mentioned above to create our result:

Conclusion

This solution performs, well, above 99% solutions of all time!

To view or add a comment, sign in

Others also viewed

Explore topics