-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_based_key_value_store.js
66 lines (62 loc) · 1.83 KB
/
time_based_key_value_store.js
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
64
65
66
// https://leetcode.com/problems/time-based-key-value-store/
// 981. Time Based Key-Value Store
// Look at binary_search.js
var TimeMap = function() {
this.store = new Map();
};
/**
* @param {string} key
* @param {string} value
* @param {number} timestamp
* @return {void}
*/
TimeMap.prototype.set = function(key, value, timestamp) {
const store = this.store;
if (!store.has(key)) store.set(key, []);
store.get(key).push([timestamp, value]);
};
/**
* @param {string} key
* @param {number} timestamp
* @return {string}
*/
TimeMap.prototype.get = function(key, timestamp) {
const bs_sourse = this.store.get(key) || [];
let output = "";
if (!bs_sourse.length) return output;
let [l, r] = [0, bs_sourse.length - 1];
while (l <= r) {
const mid = Math.floor((l + r) / 2);
const [t, v] = bs_sourse[mid];
if (timestamp === t) return v;
// right direction
if (timestamp >= t) {
l = mid + 1;
output = v;
// left direction
} else r = mid - 1;
}
return output;
};
/**
* Your TimeMap object will be instantiated and called as such:
* var obj = new TimeMap()
* obj.set(key,value,timestamp)
* var param_2 = obj.get(key,timestamp)
*/
var obj = new TimeMap()
var commands = ["get", "set", "get", "get", "set", "get", "get", "get", "get"]
var resources = [["foo", 1], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5], ["foo", 2], ["too", 1]]
// Output
// ["", undefined, "bar", "bar", undefined, "bar2", "bar2", "bar", ""]
for(let i=0; i<commands.length; i++){
const com = commands[i];
inp = resources[i];
if (com == 'set'){
output = obj.set(inp[0],inp[1],inp[2])
console.log(output)
} else {
param_2 = obj.get(inp[0],inp[1])
console.log(param_2)
}
}