Cover Sorted Unique Integers with Minimal Exact Ranges

Quick Overview

Summarize sorted unique integers as maximal exact ranges with safe full-width endpoint comparisons.

Cover Sorted Unique Integers with Minimal Exact Ranges

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a sorted array of distinct integers, return the smallest sorted list of inclusive ranges covering exactly its values. No input value may be covered twice and no absent integer may be included. Format a singleton as `"a"` and a longer range as `"a->b"`. ```cpp vector<string> summaryRanges(vector<int>& nums); ``` ### Examples ```text Input: nums = [-3,-2,0,1,2,7] Output: ["-3->-2","0->2","7"] ``` ```text Input: nums = [-2147483648,-2147483647,2147483647] Output: ["-2147483648->-2147483647","2147483647"] ``` ### Constraints - Length is between 0 and 20. - Values span the full signed 32-bit range. - All values are unique and in increasing order.

Overview: Summarize sorted unique integers as maximal exact ranges with safe full-width endpoint comparisons.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a sorted array of distinct integers, return the smallest sorted list of inclusive ranges covering exactly its values. No input value may be covered twice and no absent integer may be included. Format a singleton as "a" and a longer range as "a->b".

vector<string> summaryRanges(vector<int>& nums);

Examples

Input: nums = [-3,-2,0,1,2,7]
Output: ["-3->-2","0->2","7"]
Input: nums = [-2147483648,-2147483647,2147483647]
Output: ["-2147483648->-2147483647","2147483647"]

Constraints

  • Length is between 0 and 20.
  • Values span the full signed 32-bit range.
  • All values are unique and in increasing order.