Solution 1 binary search + simulate
t-complexity: $O(n^2 * logn)$
s-complexity: $O(n)$
Maintain a disjoint 2-d array. When add a new number, simulate according to description.
Use an unordered_set to remember a number has been added to stream. Duplicate nubmer won't change the outcome.
When add number val, there're four cases:
val-1andval+1is already in stream, then the intervals than ends withval-1and starts withval+1can be merged in to one.val-1is in stream but notval+1, just add val to interval that ends withval-1.val+1is in stream but notval-1, this case is symmetry to case 2.Both
val-1andval+1are not in stream, val will form a new interval.
Last updated
Was this helpful?