-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummaryRanges.java
More file actions
57 lines (49 loc) · 1.12 KB
/
SummaryRanges.java
File metadata and controls
57 lines (49 loc) · 1.12 KB
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
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class SummaryRanges
{
Map<Integer, Integer> map = new HashMap<>(), range = new HashMap<>();
/** Initialize your data structure here. */
public SummaryRanges()
{
}
public void addNum( int val )
{
for ( int k : range.keySet() )
{
if ( k <= val && val <= range.get( k ) )
return;
}
int head = map.containsKey( val - 1 ) ? map.get( val - 1 ) : 0,
tail = map.containsKey( val + 1 ) ? map.get( val + 1 ) : 0,
sum = head + tail + 1;
map.put( val, sum );
if ( head > 0 )
{
map.put( val - head, sum );
map.remove( val - 1 );
range.put( val - head, sum );
}
map.put( val + tail, sum );
}
public List<Interval> getIntervals()
{
return null;
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/