-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_981.java
40 lines (33 loc) · 1.16 KB
/
_981.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
package com.fishercoder.solutions.firstthousand;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class _981 {
public static class Solution1 {
public static class TimeMap {
Map<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 (!map.containsKey(key)) {
map.put(key, new TreeMap<>());
}
TreeMap<Integer, String> timestampMap = map.get(key);
timestampMap.put(timestamp, value);
}
public String get(String key, int timestamp) {
TreeMap<Integer, String> timestampMap = map.get(key);
Integer prevTimestamp = timestampMap.floorKey(timestamp);
if (prevTimestamp == null) {
return "";
} else {
return timestampMap.get(prevTimestamp);
}
}
}
}
}