-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeMap.java
More file actions
39 lines (32 loc) · 921 Bytes
/
Copy pathTimeMap.java
File metadata and controls
39 lines (32 loc) · 921 Bytes
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
package OOD;
import com.sun.source.tree.Tree;
import java.util.*;
//use 2 maps, otherwise it will exceed time limit
public class TimeMap {
HashMap<String, TreeMap<Integer, String>> map;
/** Initialize your data structure here. */
public TimeMap() {
this.map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
if (!this.map.containsKey(key)) {
map.put(key, new TreeMap<>());
}
map.get(key).put(timestamp, value);
}
public String get(String key, int timestamp) {
if (!map.containsKey(key)) {
return "";
}
else {
TreeMap<Integer, String> t = map.get(key);
Integer floor = t.floorKey(timestamp);
if (floor == null){
return "";
}
else {
return t.get(floor);
}
}
}
}