Implement a data structure supporting the following operations:
Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string.
Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string.
GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "".
GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string "".
Challenge: Perform all these in O(1) time complexity.
class AllOne {
public:
/** Initialize your data structure here. */
AllOne() {}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
void inc(string key) {
if (!mp.count(key)) {
if (buckets.empty() || buckets.back().val != 1) {
auto newBucket = buckets.insert(buckets.end(), {1, {key}});
mp[key] = newBucket;
} else {
auto newBucket = --buckets.end();
newBucket->keys.insert(key);
mp[key] = newBucket;
}
} else {
auto curBucket = mp[key], lastBucket = --mp[key];
if (lastBucket == buckets.end() || lastBucket->val != curBucket->val + 1) {
auto newBucket = buckets.insert(curBucket, {curBucket->val + 1, {key}});
mp[key] = newBucket;
} else {
lastBucket->keys.insert(key);
mp[key] = lastBucket;
}
curBucket->keys.erase(key);
if (curBucket->keys.empty()) buckets.erase(curBucket);
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
if (!mp.count(key)) return;
auto curBucket = mp[key];
if (curBucket->val == 1) {
curBucket->keys.erase(key);
if (curBucket->keys.empty()) buckets.erase(curBucket);
mp.erase(key);
return;
}
auto nextBucket = ++mp[key];
if (nextBucket == buckets.end() || nextBucket->val != curBucket->val - 1) {
auto newBucket = buckets.insert(nextBucket, {curBucket->val - 1, {key}});
mp[key] = newBucket;
} else {
nextBucket->keys.insert(key);
mp[key] = nextBucket;
}
curBucket->keys.erase(key);
if (curBucket->keys.empty()) buckets.erase(curBucket);
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
return buckets.empty() ? "" : *(buckets.begin()->keys.begin());
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
return buckets.empty() ? "" : *(buckets.rbegin()->keys.begin());
}
private:
struct Bucket {
int val;
unordered_set<string> keys;
};
list<Bucket> buckets;
unordered_map<string, list<Bucket>::iterator> mp;
};