-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeMap.java
59 lines (51 loc) · 1.82 KB
/
TimeMap.java
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
package org.sean.array;
import java.util.*;
// 981. Time Based Key-Value Store
public class TimeMap {
private static final String EMPTY = "";
private HashMap<String, SortedMap<Integer, String>> map;
/** Initialize your data structure here. */
public TimeMap() {
map = new HashMap<>();
}
// [foo, bar 1]
// [foo, bar 3]
// [foo, bar2, 4]
// -> (foo, 1) // (foo, 3) // (foo, 4) // (foo, 5)
public void set(String key, String value, int timestamp) {
if (map.containsKey(key)) {
SortedMap<Integer, String> valMap = map.get(key);
valMap.put(timestamp, value); // with timestamps increasing strictly
} else {
TreeMap<Integer, String> subMap = new TreeMap<>();
subMap.put(timestamp, value);
map.put(key, subMap);
}
}
public String get(String key, int timestamp) {
if (!map.containsKey(key)) return EMPTY;
TreeMap<Integer, String> sortedMap = (TreeMap<Integer, String>) map.get(key);
if (sortedMap.containsKey(timestamp)) {
return sortedMap.get(timestamp);
} else {
Integer lowerKey = sortedMap.lowerKey(timestamp);
if (lowerKey == null) {
return EMPTY;
} else {
return sortedMap.get(lowerKey);
}
}
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
/*Note:
All key/value strings are lowercase.
All key/value strings have length in the range [1, 100]
The timestamps for all TimeMap.set operations are strictly increasing.
1 <= timestamp <= 10^7
TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.*/