Skip to content

Commit

Permalink
Create: 981-Time-Based-Key-Value-Store.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
andra961 committed Sep 5, 2022
1 parent 2df0564 commit 92d54b8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions typescript/981-Time-Based-Key-Value-Store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//use an hashmap for keys, then each key will have an array (increasing order because of problem rules) of timestamps with values (represented as javascript objects {key, value})
class TimeMap {
public hash: {};

constructor() {
this.hash = {};
}

set(key: string, value: string, timestamp: number): void {
if (key in this.hash) {
this.hash[key].push({ timestamp, value });
} else this.hash[key] = [{ timestamp, value }];
}

get(key: string, timestamp: number): string {
//if key is not in the hashmap there are no timestamps nor values, return ""
if (!(key in this.hash)) return '';
let timestamps = this.hash[key];
//if there are no timestamps or the first timestamp is already bigger than target timestamp(they are sorted so all next ones will big too), return ""
if (timestamps.length === 0 || timestamps[0].timestamp > timestamp)
return '';

//starts from the first timestamp as closest
let closest = timestamps[0];

let [l, r] = [0, timestamps.length - 1];

//binary search, but
while (l <= r) {
let mid = Math.floor((l + r) / 2);

if (timestamps[mid].timestamp === timestamp)
return timestamps[mid].value;
//update closest if mid element's timestamp is still less than target timestamp
if (timestamps[mid].timestamp < timestamp)
closest = timestamps[mid];

if (timestamps[mid].timestamp < timestamp) l = mid + 1;
if (timestamps[mid].timestamp > timestamp) r = mid - 1;
}

return closest.value;
}
}

0 comments on commit 92d54b8

Please sign in to comment.