Class SummaryRanges

  • All Implemented Interfaces:

    
    public final class SummaryRanges
    
                        

    352 - Data Stream as Disjoint Intervals\.

    Hard

    Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.

    Implement the SummaryRanges class:

    • SummaryRanges() Initializes the object with an empty stream.

    • void addNum(int val) Adds the integer val to the stream.

    • int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals <code>start<sub>i</sub>, end<sub>i</sub></code>.

    Example 1:

    Input

    ["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
    [ [], [1], [], [3], [], [7], [], [2], [], [6], []]

    Output: [null, null, [1, 1], null, [1, 1, 3, 3], null, [1, 1, 3, 3, 7, 7], null, [1, 3, 7, 7], null, [1, 3, 6, 7]]

    Explanation:

    SummaryRanges summaryRanges = new SummaryRanges();
    summaryRanges.addNum(1); // arr = [1]
    summaryRanges.getIntervals(); // return [[1, 1]]
    summaryRanges.addNum(3); // arr = [1, 3]
    summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
    summaryRanges.addNum(7); // arr = [1, 3, 7]
    summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
    summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
    summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
    summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
    summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]

    Constraints:

    • <code>0 <= val <= 10<sup>4</sup></code>

    • At most <code>3 * 10<sup>4</sup></code> calls will be made to addNum and getIntervals.

    Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      SummaryRanges()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Unit addNum(Integer val)
      final Array<IntArray> getIntervals()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait