-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (25 loc) · 883 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (25 loc) · 883 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
// 981. Time Based Key-Value Store
// https://leetcode.com/problems/time-based-key-value-store/
// Medium | Java | Accepted 2026-08-10
// Runtime 164 ms | Memory 108.9 MB
class TimeMap {
private Map<String, TreeMap<Integer, String>> map;
public TimeMap() {
map = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
map.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
}
public String get(String key, int timestamp) {
TreeMap<Integer, String> tm = map.get(key);
if (tm == null) return "";
Integer e = tm.floorKey(timestamp);
return e == null ? "" : tm.get(e);
}
}
/**
* 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);
*/