|
| 1 | +# [全 O(1) 的数据结构](https://leetcode-cn.com/explore/interview/card/bytedance/245/data-structure/1033/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +实现一个数据结构支持以下操作: |
| 6 | + |
| 7 | + - Inc(key) - 插入一个新的值为 1 的 key。或者使一个存在的 key 增加一,保证 key 不为空字符串。 |
| 8 | + - Dec(key) - 如果这个 key 的值是 1,那么把他从数据结构中移除掉。否者使一个存在的 key 值减一。如果这个 key 不存在,这个函数不做任何事情。key 保证不为空字符串。 |
| 9 | + - GetMaxKey() - 返回 key 中值最大的任意一个。如果没有元素存在,返回一个空字符串""。 |
| 10 | + - GetMinKey() - 返回 key 中值最小的任意一个。如果没有元素存在,返回一个空字符串""。 |
| 11 | + |
| 12 | +挑战:以 O(1) 的时间复杂度实现所有操作。 |
| 13 | + |
| 14 | +## 解题思路 |
| 15 | + |
| 16 | + 1. 设计一个 `Bucket` 保存所有值为 `value` 的 `key` |
| 17 | + 2. 并且有临近 `value` 的 `Bucket` 指针 |
| 18 | + |
| 19 | +``` |
| 20 | +class AllOne { |
| 21 | +
|
| 22 | + /** Initialize your data structure here. */ |
| 23 | + public AllOne() { |
| 24 | +
|
| 25 | + } |
| 26 | +
|
| 27 | + private static class Bucket { |
| 28 | + private int value; |
| 29 | + private Set<String> keys = new HashSet<>(); |
| 30 | + private Bucket next; |
| 31 | + private Bucket pre; |
| 32 | +
|
| 33 | + public Bucket(int value) { |
| 34 | + this.value = value; |
| 35 | + } |
| 36 | +
|
| 37 | + @Override |
| 38 | + public String toString() { |
| 39 | + return "Bucket{" + |
| 40 | + "value=" + value + |
| 41 | + ", keys=" + keys + |
| 42 | + '}'; |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + private Map<String, Bucket> data = new HashMap<>(); |
| 47 | + private List<Bucket> bucketList = new ArrayList<>(); |
| 48 | +
|
| 49 | + /** |
| 50 | + * Inserts a new key <Key> with value 1. Or increments an existing key by 1. |
| 51 | + */ |
| 52 | + public void inc(String key) { |
| 53 | + if (data.containsKey(key)) { |
| 54 | + Bucket bucket = data.get(key); |
| 55 | + bucket.keys.remove(key); |
| 56 | +
|
| 57 | + if (bucket.next == null) { |
| 58 | + bucket.next = new Bucket(bucket.value + 1); |
| 59 | + bucket.next.pre = bucket; |
| 60 | + bucketList.add(bucket.next); |
| 61 | + } |
| 62 | +
|
| 63 | + bucket.next.keys.add(key); |
| 64 | + data.put(key, bucket.next); |
| 65 | + } else { |
| 66 | + if (bucketList.size() == 0) { |
| 67 | + bucketList.add(new Bucket(1)); |
| 68 | + } |
| 69 | +
|
| 70 | + Bucket bucket = bucketList.get(0); |
| 71 | + bucket.keys.add(key); |
| 72 | + data.put(key, bucket); |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + /** |
| 77 | + * Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. |
| 78 | + */ |
| 79 | + public void dec(String key) { |
| 80 | + if (!data.containsKey(key)) { |
| 81 | + return; |
| 82 | + } |
| 83 | +
|
| 84 | + Bucket bucket = data.get(key); |
| 85 | + if (bucket.pre == null) { |
| 86 | + bucket.keys.remove(key); |
| 87 | + data.remove(key); |
| 88 | + } else { |
| 89 | + bucket.keys.remove(key); |
| 90 | + bucket.pre.keys.add(key); |
| 91 | + data.put(key, bucket.pre); |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | + /** |
| 96 | + * Returns one of the keys with maximal value. |
| 97 | + */ |
| 98 | + public String getMaxKey() { |
| 99 | + if (bucketList.size() == 0) { |
| 100 | + return ""; |
| 101 | + } |
| 102 | +
|
| 103 | + for (int i = bucketList.size() - 1; i >= 0; i--) { |
| 104 | + Bucket bucket = bucketList.get(i); |
| 105 | +
|
| 106 | + if (!bucket.keys.isEmpty()) { |
| 107 | + Iterator<String> iterator = bucket.keys.iterator(); |
| 108 | + if (iterator.hasNext()) { |
| 109 | + return iterator.next(); |
| 110 | + } else { |
| 111 | + return ""; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +
|
| 116 | + return ""; |
| 117 | + } |
| 118 | +
|
| 119 | + /** |
| 120 | + * Returns one of the keys with Minimal value. |
| 121 | + */ |
| 122 | + public String getMinKey() { |
| 123 | + if (bucketList.size() == 0) { |
| 124 | + return ""; |
| 125 | + } |
| 126 | +
|
| 127 | + for (Bucket bucket : bucketList) { |
| 128 | + if (!bucket.keys.isEmpty()) { |
| 129 | + Iterator<String> iterator = bucket.keys.iterator(); |
| 130 | + if (iterator.hasNext()) { |
| 131 | + return iterator.next(); |
| 132 | + } else { |
| 133 | + return ""; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + return ""; |
| 139 | + } |
| 140 | +} |
| 141 | +
|
| 142 | +``` |
0 commit comments