|
| 1 | +// 時間複雜度: |
| 2 | +// 空間複雜度: |
| 3 | +/* |
| 4 | + * @lc app=leetcode.cn id=981 lang=golang |
| 5 | + * |
| 6 | + * [981] 基于时间的键值存储 |
| 7 | + * |
| 8 | + * https://leetcode.cn/problems/time-based-key-value-store/description/ |
| 9 | + * |
| 10 | + * algorithms |
| 11 | + * Medium (52.55%) |
| 12 | + * Likes: 226 |
| 13 | + * Dislikes: 0 |
| 14 | + * Total Accepted: 32.9K |
| 15 | + * Total Submissions: 62.7K |
| 16 | + * Testcase Example: '["TimeMap","set","get","get","set","get","get"]\n' + |
| 17 | + '[[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]' |
| 18 | + * |
| 19 | + * 设计一个基于时间的键值数据结构,该结构可以在不同时间戳存储对应同一个键的多个值,并针对特定时间戳检索键对应的值。 |
| 20 | + * |
| 21 | + * 实现 TimeMap 类: |
| 22 | + * |
| 23 | + * |
| 24 | + * TimeMap() 初始化数据结构对象 |
| 25 | + * void set(String key, String value, int timestamp) 存储给定时间戳 timestamp 时的键 key |
| 26 | + * 和值 value。 |
| 27 | + * String get(String key, int timestamp) 返回一个值,该值在之前调用了 set,其中 timestamp_prev |
| 28 | + * <= timestamp 。如果有多个这样的值,它将返回与最大 timestamp_prev 关联的值。如果没有值,则返回空字符串("")。 |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * 示例 1: |
| 33 | + * |
| 34 | + * |
| 35 | + * 输入: |
| 36 | + * ["TimeMap", "set", "get", "get", "set", "get", "get"] |
| 37 | + * [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", |
| 38 | + * 4], ["foo", 5]] |
| 39 | + * 输出: |
| 40 | + * [null, null, "bar", "bar", null, "bar2", "bar2"] |
| 41 | + * |
| 42 | + * 解释: |
| 43 | + * TimeMap timeMap = new TimeMap(); |
| 44 | + * timeMap.set("foo", "bar", 1); // 存储键 "foo" 和值 "bar" ,时间戳 timestamp = 1 |
| 45 | + * timeMap.get("foo", 1); // 返回 "bar" |
| 46 | + * timeMap.get("foo", 3); // 返回 "bar", 因为在时间戳 3 和时间戳 2 处没有对应 "foo" |
| 47 | + * 的值,所以唯一的值位于时间戳 1 处(即 "bar") 。 |
| 48 | + * timeMap.set("foo", "bar2", 4); // 存储键 "foo" 和值 "bar2" ,时间戳 timestamp = 4 |
| 49 | + * timeMap.get("foo", 4); // 返回 "bar2" |
| 50 | + * timeMap.get("foo", 5); // 返回 "bar2" |
| 51 | + * |
| 52 | + * |
| 53 | + * |
| 54 | + * |
| 55 | + * 提示: |
| 56 | + * |
| 57 | + * |
| 58 | + * 1 <= key.length, value.length <= 100 |
| 59 | + * key 和 value 由小写英文字母和数字组成 |
| 60 | + * 1 <= timestamp <= 10^7 |
| 61 | + * set 操作中的时间戳 timestamp 都是严格递增的 |
| 62 | + * 最多调用 set 和 get 操作 2 * 10^5 次 |
| 63 | + * |
| 64 | + * |
| 65 | +*/ |
| 66 | + |
| 67 | +// @lc code=start |
| 68 | + |
| 69 | +type element struct { |
| 70 | + key string |
| 71 | + value string |
| 72 | + timestamp int |
| 73 | +} |
| 74 | + |
| 75 | +type TimeMap struct { |
| 76 | + elements map[string][]element |
| 77 | +} |
| 78 | + |
| 79 | +func Constructor() TimeMap { |
| 80 | + return TimeMap{elements: make(map[string][]element)} |
| 81 | +} |
| 82 | + |
| 83 | +func (this *TimeMap) Set(key string, value string, timestamp int) { |
| 84 | + this.elements[key] = append(this.elements[key], element{ |
| 85 | + key: key, |
| 86 | + value: value, |
| 87 | + timestamp: timestamp, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func (this *TimeMap) Get(key string, timestamp int) string { |
| 92 | + if _, ok := this.elements[key]; !ok { |
| 93 | + return "" |
| 94 | + } |
| 95 | + // 二分法找出符合條件的最左邊 |
| 96 | + index := sort.Search(len(this.elements[key]), func(i int) bool { |
| 97 | + return this.elements[key][i].timestamp > timestamp |
| 98 | + }) |
| 99 | + if index == 0 { |
| 100 | + return "" |
| 101 | + } |
| 102 | + |
| 103 | + index-- |
| 104 | + return this.elements[key][index].value |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Your TimeMap object will be instantiated and called as such: |
| 109 | + * obj := Constructor(); |
| 110 | + * obj.Set(key,value,timestamp); |
| 111 | + * param_2 := obj.Get(key,timestamp); |
| 112 | + */ |
| 113 | +// @lc code=end |
| 114 | + |
0 commit comments