Skip to content

Commit 28fa373

Browse files
committed
leetcode
1 parent a4933d8 commit 28fa373

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7373
- [Minimum Time to Make Rope Colorful](MinimumTimeToMakeRopeColorful/minimum_time_to_make_rope_colorful.dart)
7474
- [Isomorphic Strings](IsomorphicStrings/isomorphic_strings.dart)
7575
- [Add One Row to Tree](AddOneRowToTree/add_one_row_to_tree.dart)
76+
- [Time Based Key-Value Store](TimeBasedKeyValueStore/time_based_key_value_store.dart)
7677

7778
## Reach me via
7879

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
3+
4+
-* Time Based Key-Value Store *-
5+
6+
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.
7+
8+
Implement the TimeMap class:
9+
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 at the given time timestamp.
12+
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 "".
13+
14+
15+
Example 1:
16+
17+
Input
18+
["TimeMap", "set", "get", "get", "set", "get", "get"]
19+
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
20+
Output
21+
[null, null, "bar", "bar", null, "bar2", "bar2"]
22+
23+
Explanation
24+
TimeMap timeMap = new TimeMap();
25+
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
26+
timeMap.get("foo", 1); // return "bar"
27+
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
28+
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
29+
timeMap.get("foo", 4); // return "bar2"
30+
timeMap.get("foo", 5); // return "bar2"
31+
32+
33+
Constraints:
34+
35+
1 <= key.length, value.length <= 100
36+
key and value consist of lowercase English letters and digits.
37+
1 <= timestamp <= 107
38+
All the timestamps timestamp of set are strictly increasing.
39+
At most 2 * 105 calls will be made to set and get.
40+
41+
*/
42+
43+
//Your TimeMap object will be instantiated and called as such:
44+
45+
// class Pair<T1,T2> {
46+
// final T1 first;
47+
// final T2 second;
48+
// Pair(this.first, this.second);
49+
50+
// bool operator == (final Pair other) {
51+
// return first == other.first && second == other.second;
52+
// }
53+
// int get hashCode => hash(first.hashCode,second.hashCode);
54+
// }
55+
56+
// class Pair {
57+
// late String value;
58+
// late int time;
59+
// Pair(String _value, int _timestamp) {
60+
// time = _timestamp;
61+
// value = _value;
62+
// }
63+
// }
64+
65+
// class F {
66+
// List<Pair> list = <Pair>[];
67+
68+
// F(String _value, int _timestamp) {
69+
// list.add(Pair(_value, _timestamp));
70+
// }
71+
// F.from(List<Pair> c) {
72+
// list = c;
73+
// }
74+
// }
75+
76+
class TimeMap {
77+
Map map = {};
78+
TimeMap() {
79+
this.map;
80+
}
81+
82+
void set(String key, String value, int timestamp) {}
83+
84+
String get(String key, int timestamp) {
85+
return "";
86+
}
87+
}
88+
89+
// TimeMap obj = TimeMap();
90+
// obj.set(key,value,timestamp);
91+
// String param2 = obj.get(key,timestamp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import "strconv"
4+
5+
//Runtime: 454 ms, faster than 85.83% of Go online submissions for Time Based Key-Value Store.
6+
//Memory Usage: 73.3 MB, less than 10.52% of Go online submissions for Time Based Key-Value Store.
7+
8+
type TimeMap struct {
9+
timestamps map[string][]int
10+
values map[string]string
11+
}
12+
13+
// Constructor initializes TimeMap structure
14+
func Constructor() TimeMap {
15+
return TimeMap{
16+
timestamps: make(map[string][]int),
17+
values: make(map[string]string),
18+
}
19+
}
20+
21+
// Set value by key and timestamp
22+
func (tm *TimeMap) Set(key string, value string, timestamp int) {
23+
tm.timestamps[key] = append(tm.timestamps[key], timestamp)
24+
tm.values[key+strconv.Itoa(timestamp)] = value
25+
}
26+
27+
// Get value by key and timestamp
28+
func (tm *TimeMap) Get(key string, timestamp int) string {
29+
timestamps, ok := tm.timestamps[key]
30+
if !ok || timestamps[0] > timestamp {
31+
return ""
32+
}
33+
if timestamps[len(timestamps)-1] <= timestamp {
34+
return tm.values[key+strconv.Itoa(timestamps[len(timestamps)-1])]
35+
}
36+
left := 0
37+
right := len(timestamps) - 1
38+
for left < right {
39+
mid := left + (right-left)/2
40+
if timestamps[mid] == timestamp {
41+
return tm.values[key+strconv.Itoa(timestamps[mid])]
42+
}
43+
if timestamps[mid] < timestamp {
44+
left = mid + 1
45+
} else {
46+
right = mid
47+
}
48+
}
49+
return tm.values[key+strconv.Itoa(timestamps[left-1])]
50+
}

TimeBasedKeyValueStore/time_based_key_value_store.md

Whitespace-only changes.

0 commit comments

Comments
 (0)