-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0352-DataStreamAsDisjointIntervals.cs
59 lines (52 loc) · 1.93 KB
/
0352-DataStreamAsDisjointIntervals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//-----------------------------------------------------------------------------
// Runtime: 332ms
// Memory Usage: 43.6 MB
// Link: https://leetcode.com/submissions/detail/382308922/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _0352_DataStreamAsDisjointIntervals
{
private readonly SortedDictionary<int, int[]> map;
/** Initialize your data structure here. */
public _0352_DataStreamAsDisjointIntervals()
{
map = new SortedDictionary<int, int[]>();
}
public void AddNum(int val)
{
if (map.ContainsKey(val)) return;
var keys = map.Keys.ToArray();
var indexHi = Array.BinarySearch(keys, val);
indexHi = ~indexHi;
var indexLo = indexHi - 1;
if (indexLo >= 0 && indexLo < keys.Length && indexHi < keys.Length && map[keys[indexLo]][1] + 1 == val && keys[indexHi] == val + 1)
{
map[keys[indexLo]][1] = map[keys[indexHi]][1];
map.Remove(keys[indexHi]);
}
else if (indexLo >= 0 && indexLo < keys.Length && map[keys[indexLo]][1] + 1 >= val)
map[keys[indexLo]][1] = Math.Max(map[keys[indexLo]][1], val);
else if (indexHi < keys.Length && keys[indexHi] == val + 1)
{
map[val] = new int[] { val, map[keys[indexHi]][1] };
map.Remove(keys[indexHi]);
}
else
map[val] = new int[] { val, val };
}
public int[][] GetIntervals()
{
return map.Values.ToArray();
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.AddNum(val);
* int[][] param_2 = obj.GetIntervals();
*/
}