-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
58 lines (52 loc) · 1.61 KB
/
Main.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
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
TimeMap map = new TimeMap();
map.set("foo", "bar", 1);
System.out.println(map.get("foo", 1));
System.out.println(map.get("foo", 3));
map.set("foo", "bar2", 4);
System.out.println(map.get("foo", 4));
System.out.println(map.get("foo", 5));
}
}
class Data {
String val;
int time;
Data(String val, int time) {
this.val = val;
this.time = time;
}
}
class TimeMap {
Map<String, List<Data>> map;
public TimeMap() {
map = new HashMap<String, List<Data>>();
}
public void set(String key, String value, int timestamp) {
if (!map.containsKey(key))
map.put(key, new ArrayList<Data>());
map.get(key).add(new Data(value, timestamp));
}
public String get(String key, int timestamp) {
if (!map.containsKey(key))
return "";
return binarySearch(map.get(key), timestamp);
}
private String binarySearch(List<Data> list, int timestamp) {
int low = 0, high = list.size() - 1;
while (low < high) {
int mid = (low + high) / 2;
if (list.get(mid).time == timestamp)
return list.get(mid).val;
if (list.get(mid).time < timestamp) {
if (list.get(mid + 1).time > timestamp)
return list.get(mid).val;
low = mid + 1;
} else
high = mid - 1;
}
return list.get(low).time <= timestamp ? list.get(low).val : "";
}
}