Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec3ec9b

Browse files
committedMar 29, 2025
Add solution #981
1 parent e43ff27 commit ec3ec9b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,063 LeetCode solutions in JavaScript
1+
# 1,064 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -789,6 +789,7 @@
789789
978|[Longest Turbulent Subarray](./solutions/0978-longest-turbulent-subarray.js)|Medium|
790790
979|[Distribute Coins in Binary Tree](./solutions/0979-distribute-coins-in-binary-tree.js)|Medium|
791791
980|[Unique Paths III](./solutions/0980-unique-paths-iii.js)|Hard|
792+
981|[Time Based Key-Value Store](./solutions/0981-time-based-key-value-store.js)|Medium|
792793
985|[Sum of Even Numbers After Queries](./solutions/0985-sum-of-even-numbers-after-queries.js)|Easy|
793794
989|[Add to Array-Form of Integer](./solutions/0989-add-to-array-form-of-integer.js)|Easy|
794795
994|[Rotting Oranges](./solutions/0994-rotting-oranges.js)|Medium|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 981. Time Based Key-Value Store
3+
* https://leetcode.com/problems/time-based-key-value-store/
4+
* Difficulty: Medium
5+
*
6+
* Design a time-based key-value data structure that can store multiple values for the same key
7+
* at different time stamps and retrieve the key's value at a certain timestamp.
8+
*
9+
* Implement the TimeMap class:
10+
* - TimeMap() Initializes the object of the data structure.
11+
* - void set(String key, String value, int timestamp) Stores the key key with the value value
12+
* at the given time timestamp.
13+
* - String get(String key, int timestamp) Returns a value such that set was called previously,
14+
* with timestamp_prev <= timestamp. If there are multiple such values, it returns the value
15+
* associated with the largest timestamp_prev. If there are no values, it returns "".
16+
*/
17+
18+
var TimeMap = function() {
19+
this.store = new Map();
20+
};
21+
22+
/**
23+
* @param {string} key
24+
* @param {string} value
25+
* @param {number} timestamp
26+
* @return {void}
27+
*/
28+
TimeMap.prototype.set = function(key, value, timestamp) {
29+
if (!this.store.has(key)) this.store.set(key, []);
30+
this.store.get(key).push([timestamp, value]);
31+
};
32+
33+
/**
34+
* @param {string} key
35+
* @param {number} timestamp
36+
* @return {string}
37+
*/
38+
TimeMap.prototype.get = function(key, timestamp) {
39+
if (!this.store.has(key)) return '';
40+
const entries = this.store.get(key);
41+
let left = 0;
42+
let right = entries.length - 1;
43+
44+
while (left <= right) {
45+
const mid = Math.floor((left + right) / 2);
46+
const [midTime, midValue] = entries[mid];
47+
48+
if (midTime === timestamp) return midValue;
49+
if (midTime < timestamp) left = mid + 1;
50+
else right = mid - 1;
51+
}
52+
53+
return right >= 0 ? entries[right][1] : '';
54+
};

0 commit comments

Comments
 (0)
Please sign in to comment.