-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimeMap.java
More file actions
63 lines (55 loc) · 1.63 KB
/
TimeMap.java
File metadata and controls
63 lines (55 loc) · 1.63 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
58
59
60
61
62
63
import java.util.*;
public class TimeMap {
class Pair{
int time;
String value;
Pair(int time, String value){
this.time = time;
this.value = value;
}
}
Map<String, List<Pair>> map;
public TimeMap() {
map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
map.putIfAbsent(key, new ArrayList<>());
List<Pair> treeMap = map.get(key);
treeMap.add(new Pair(timestamp, value));
}
public String get(String key, int timestamp) {
List<Pair> treeMap = map.get(key);
int res = binarySearch(treeMap, timestamp);
if(res == -1) return "";
else{
return treeMap.get(res).value;
}
}
public int binarySearch(List<Pair> list, int target){
//System.out.println("list :" + list);
int left = 0;
int right = list.size()-1;
int mid;
while(left+1 < right){
mid = left + (right-left)/2;
if(list.get(mid).time < target){
left ++;
}else{
right --;
}
}
if(list.get(left).time > target) return -1;
if(list.get(right).time <= target) return right;
else{
return left;
}
}
public static void main(String[] args){
TimeMap timeMap = new TimeMap();
timeMap.set("love", "high", 10);
timeMap.set("love", "low", 20);
System.out.println(timeMap.get("love", 10));
System.out.println(timeMap.get("love", 15));
System.out.println(timeMap.get("love", 20));
}
}