Problem solution · C++

All Oone Data Structure

All Oone Data Structure: a C++ solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Hash-based lookup
Source
Kamyu LeetCode Solutions
Length
80 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For All Oone Data Structure, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 80 lines of C++ from the credited upstream file all-oone-data-structure.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • No explicit loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeAll Oone Data Structure · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(1), per operation// Space: O(k) 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 (!bucketOfKey_.count(key)) {            bucketOfKey_[key] = buckets_.insert(buckets_.begin(), {0, {key}});        }                    auto next = bucketOfKey_[key], bucket = next++;        if (next == buckets_.end() || next->value > bucket->value + 1) {            next = buckets_.insert(next, {bucket->value + 1, {}});        }        next->keys.insert(key);        bucketOfKey_[key] = next;                bucket->keys.erase(key);        if (bucket->keys.empty()) {            buckets_.erase(bucket);        }    }        /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */    void dec(string key) {        if (!bucketOfKey_.count(key)) {            return;        }         auto prev = bucketOfKey_[key], bucket = prev--;        bucketOfKey_.erase(key);        if (bucket->value > 1) {            if (bucket == buckets_.begin() || prev->value < bucket->value - 1) {                prev = buckets_.insert(bucket, {bucket->value - 1, {}});            }            prev->keys.insert(key);            bucketOfKey_[key] = prev;        }                bucket->keys.erase(key);        if (bucket->keys.empty()) {            buckets_.erase(bucket);        }    }        /** Returns one of the keys with maximal value. */    string getMaxKey() {        return buckets_.empty() ? "" : *(buckets_.rbegin()->keys.begin());    }        /** Returns one of the keys with Minimal value. */    string getMinKey() {        return buckets_.empty() ? "" : *(buckets_.begin()->keys.begin());    } private:    struct Bucket {        int value;        unordered_set<string> keys;    };    list<Bucket> buckets_;    unordered_map<string, list<Bucket>::iterator> bucketOfKey_;}; /** * Your AllOne object will be instantiated and called as such: * AllOne obj = new AllOne(); * obj.inc(key); * obj.dec(key); * string param_3 = obj.getMaxKey(); * string param_4 = obj.getMinKey(); */  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗