forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create: 981-Time-Based-Key-Value-Store.ts
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |