Skip to content

Commit 48fcae4

Browse files
authored
feat: resolve time based key value store
Feat time based key value store
2 parents 73111cd + b1e9d55 commit 48fcae4

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

src/medium/time_based_key_value_store.rs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,73 @@
11
#![allow(dead_code)]
2-
struct TimeMap {}
2+
use std::collections::HashMap;
33

44
/**
5-
* `&self` means the method takes an immutable reference.
6-
* If you need a mutable reference, change it to `&mut self` instead.
5+
* Your TimeMap object will be instantiated and called as such:
6+
* let obj = TimeMap::new();
7+
* obj.set(key, value, timestamp);
8+
* let ret_2: String = obj.get(key, timestamp);
79
*/
10+
struct TimeMap {
11+
hm: HashMap<String, Vec<(String, i32)>>,
12+
}
13+
814
impl TimeMap {
915
fn new() -> Self {
10-
TimeMap {}
16+
Self { hm: HashMap::new() }
1117
}
1218

13-
fn set(&self, key: String, value: String, timestamp: i32) {}
19+
fn set(&mut self, key: String, value: String, timestamp: i32) {
20+
self.hm.entry(key).or_default().push((value, timestamp));
21+
}
1422

1523
fn get(&self, key: String, timestamp: i32) -> String {
16-
"".to_string()
24+
let mut res = String::new();
25+
26+
if let Some(t_list) = self.hm.get(&key) {
27+
let (mut l, mut r) = (0, t_list.len());
28+
29+
while l < r {
30+
let m = l + (r - l) / 2;
31+
if timestamp < t_list[m].1 {
32+
r = m;
33+
} else {
34+
res = t_list[m].0.clone();
35+
l = m + 1;
36+
}
37+
}
38+
}
39+
40+
res
1741
}
1842
}
1943

20-
/**
21-
* Your TimeMap object will be instantiated and called as such:
22-
* let obj = TimeMap::new();
23-
* obj.set(key, value, timestamp);
24-
* let ret_2: String = obj.get(key, timestamp);
25-
*/
44+
/*
45+
Algorithm - Binary Search
46+
47+
- Create a HashMap with key as String and value as Vec<(String, i32)>
48+
- set(key, value, timestamp)
49+
- Insert the key into the HashMap if it doesn't exist
50+
- Push the value and timestamp into the Vec
51+
- get(key, timestamp)
52+
- If the key doesn't exist, return empty string
53+
- If the key exists, do binary search on the Vec<(String, i32)>
54+
- If the timestamp is less than the middle element's timestamp, search the left half
55+
- If the timestamp is greater than or equal to the middle element's timestamp, search the right half
56+
- If the timestamp is equal to the middle element's timestamp, return the value
57+
- If the timestamp is greater than the middle element's timestamp, return the value of the right element
58+
- If the timestamp is less than the middle element's timestamp, return the value of the left element
59+
60+
Time O(logN)
61+
Space O(N)
62+
*/
2663

2764
#[cfg(test)]
2865
mod tests {
2966
use super::*;
3067

3168
#[test]
32-
fn test_981() {
33-
let obj = TimeMap::new();
69+
fn test_1() {
70+
let mut obj = TimeMap::new();
3471
obj.set("foo".to_string(), "bar".to_string(), 1);
3572
assert_eq!(obj.get("foo".to_string(), 1), "bar".to_string());
3673
assert_eq!(obj.get("foo".to_string(), 3), "bar".to_string());

src/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
- [x] [739. Daily temperatures](../src/medium/daily_temperatures.rs) -> [Problem Description](../src/medium/readme.md#739-daily-temperatures)
6767
- [x] [853. Car fleet](../src/medium/car_fleet.rs) -> [Problem Description](../src/medium/readme.md#853-car-fleet)
6868
- [x] [875. Koko eating bananas](../src/medium/koko_eating_bananas.rs) -> [Problem Description](../src/medium/readme.md#875-koko-eating-bananas)
69-
- [ ] [981. Time based key-value store](../src/medium/time_based_key_value_store.rs) -> [Problem Description](../src/medium/readme.md#981-time-based-key-value-store)
69+
- [x] [981. Time based key-value store](../src/medium/time_based_key_value_store.rs) -> [Problem Description](../src/medium/readme.md#981-time-based-key-value-store)
7070
- [Hard](../src/hard)
7171
- [x] [42. Trapping rain water](../src/hard/trapping_rain_water.rs) -> [Problem Description](../src/hard/readme.md#42-trapping-rain-water)
7272
- [x] [76. Minimum window substring](../src/hard/minimum_window_substring.rs) -> [Problem Description](../src/hard/readme.md#76-minimum-window-substring)

theory/categories/3.binary_search/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
- [x] [koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | Medium | [Solution](../../../src/medium/koko_eating_bananas.rs) | [Problem Description](../../../src/medium/readme.md#875-koko-eating-bananas)
3434
- [x] [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | Medium | [Solution](../../../src/medium/find_minimum_in_rotated_sorted_array.rs) | [Problem Description](../../../src/medium/readme.md#153-find-minimum-in-rotated-sorted-array)
3535
- [x] [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium | [Solution](../../../src/medium/search_in_rotated_sorted_array.rs)
36-
- [ ] [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | Medium | [Solution](../../../src/medium/time_based_key_value_store.rs) | [Problem Description](../../../src/medium/readme.md#981-time-based-key-value-store)
36+
- [x] [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | Medium | [Solution](../../../src/medium/time_based_key_value_store.rs) | [Problem Description](../../../src/medium/readme.md#981-time-based-key-value-store)
3737
- [ ] [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | Hard | [Solution](../../../src/hard/median_of_two_sorted_arrays.rs)
3838

3939
Category: `Binary Search`
40-
Created on: 2023-09-21 01:00:00
41-
Last modified on: 2023-09-30 10:30:00
40+
Created on: 2023-09-21 01:00
41+
Last modified on: 2023-10-4 11:50
4242
Status: In Progress
4343
Author: [David Bujosa](https://github.com/bujosa)

0 commit comments

Comments
 (0)