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-1
andval+1
is already in stream, then the intervals than ends withval-1
and starts withval+1
can be merged in to one.val-1
is in stream but notval+1
, just add val to interval that ends withval-1
.val+1
is in stream but notval-1
, this case is symmetry to case 2.Both
val-1
andval+1
are not in stream, val will form a new interval.
Last updated