java.lang.Object
g0301_0400.s0352_data_stream_as_disjoint_intervals.SummaryRanges

public class SummaryRanges extends Object
352 - Data Stream as Disjoint Intervals.<p>Hard</p> <p>Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, &hellip;, a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.</p> <p>Implement the <code>SummaryRanges</code> class:</p> <ul> <li><code>SummaryRanges()</code> Initializes the object with an empty stream.</li> <li><code>void addNum(int val)</code> Adds the integer <code>val</code> to the stream.</li> <li><code>int[][] getIntervals()</code> 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>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <pre><code> [&quot;SummaryRanges&quot;, &quot;addNum&quot;, &quot;getIntervals&quot;, &quot;addNum&quot;, &quot;getIntervals&quot;, &quot;addNum&quot;, &quot;getIntervals&quot;, &quot;addNum&quot;, &quot;getIntervals&quot;, &quot;addNum&quot;, &quot;getIntervals&quot;] [ [], [1], [], [3], [], [7], [], [2], [], [6], []] </code></pre> <p><strong>Output:</strong> [null, null, <a href="1,-1">1, 1</a>, null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]</p> <p><strong>Explanation:</strong></p> <pre><code> 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]] </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= val <= 10<sup>4</sup></code></li> <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>addNum</code> and <code>getIntervals</code>.</li> </ul> <p><strong>Follow up:</strong> What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?</p>
  • Constructor Details

    • SummaryRanges

      public SummaryRanges()
  • Method Details

    • addNum

      public void addNum(int val)
    • getIntervals

      public int[][] getIntervals()