-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0981-time-based-key-value-store.js
54 lines (48 loc) · 1.65 KB
/
0981-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
/**
* 981. Time Based Key-Value Store
* https://leetcode.com/problems/time-based-key-value-store/
* Difficulty: Medium
*
* Design a time-based key-value data structure that can store multiple values for the same key
* at different time stamps and retrieve the key's value at a certain timestamp.
*
* Implement the TimeMap class:
* - TimeMap() Initializes the object of the data structure.
* - void set(String key, String value, int timestamp) Stores the key key with the value value
* at the given time timestamp.
* - String get(String key, int timestamp) Returns a value such that set was called previously,
* with timestamp_prev <= timestamp. If there are multiple such values, it returns the value
* associated with the largest timestamp_prev. If there are no values, it returns "".
*/
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) {
if (!this.store.has(key)) this.store.set(key, []);
this.store.get(key).push([timestamp, value]);
};
/**
* @param {string} key
* @param {number} timestamp
* @return {string}
*/
TimeMap.prototype.get = function(key, timestamp) {
if (!this.store.has(key)) return '';
const entries = this.store.get(key);
let left = 0;
let right = entries.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const [midTime, midValue] = entries[mid];
if (midTime === timestamp) return midValue;
if (midTime < timestamp) left = mid + 1;
else right = mid - 1;
}
return right >= 0 ? entries[right][1] : '';
};