Show problems as
Classic problem #228EasyArray

Summary Ranges

Real-world version: Retained Revision Report
Tracked in this browser only
Read Summary Ranges on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A build server stamps every artifact it publishes with a signed 32-bit revision number, and a background job prunes old ones, so what is left on disk has holes in it.

revisions holds the revision numbers still on disk, strictly increasing with no repeats. Gather them into stretches of consecutive numbers and describe each stretch with one string:

  • a stretch running from a through b where b is larger than a is written "a..b"
  • a stretch holding only the number a is written "a"

Return the descriptions in increasing revision order. Each retained revision must belong to exactly one stretch, and no stretch may stop while the very next number is also on disk, so the report is as short as it can be.

Examples

Example 1

Input
revisions = [-9, -8, -7, -3, 0, 1, 4]
Output
["-9..-7", "-3", "0..1", "4"]

-9, -8 and -7 follow one another, so one description covers them. -3 sits alone because neither -4 nor -2 is on disk. 0 and 1 pair up, and 4 sits alone.

Example 2

Input
revisions = []
Output
[]

Nothing is on disk, so the report is empty.

Example 3

Input
revisions = [2147483645, 2147483646, 2147483647]
Output
["2147483645..2147483647"]

The three largest revision numbers follow one another and collapse into a single description.

Constraints

  • 0 <= revisions.length <= 20
  • -2^31 <= revisions[i] <= 2^31 - 1
  • All the values of revisions are unique.
  • revisions is sorted in ascending order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def retained_revision_report(revisions: list[int]) -> list[str]:
Java
public List<String> retainedRevisionReport(int[] revisions)