Problem solution · C++

All O`one Data Structure

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

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
89 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 O`one 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

  • 89 lines of C++ from the credited upstream file 432.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 walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeAll O`one Data Structure · C++C++
Use this to learn the idea, then write your own version.
struct Node {  int count;  unordered_set<string> keys;}; class AllOne { public:  void inc(string key) {    if (const auto it = keyToIterator.find(key); it == keyToIterator.end())      addNewKey(key);    else      incrementExistingKey(it, key);  }   void dec(string key) {    const auto it = keyToIterator.find(key);    // It is guaranteed that key exists in the data structure before the    // decrement.    decrementExistingKey(it, key);  }   string getMaxKey() {    return nodeList.empty() ? "" : *nodeList.back().keys.begin();  }   string getMinKey() {    return nodeList.empty() ? "" : *nodeList.front().keys.begin();  }  private:  list<Node> nodeList;  // list of nodes sorted by count  unordered_map<string, list<Node>::iterator> keyToIterator;   // Adds a new node with count 1.  void addNewKey(const string& key) {    if (nodeList.empty() || nodeList.front().count > 1)      nodeList.push_front({1, {key}});    else  // nodeList.front().count == 1      nodeList.front().keys.insert(key);    keyToIterator[key] = nodeList.begin();  }   // Increments the count of the key by 1.  void incrementExistingKey(      unordered_map<string, list<Node>::iterator>::iterator it,      const string& key) {    const auto listIt = it->second;     auto nextIt = next(listIt);    const int newCount = listIt->count + 1;    if (nextIt == nodeList.end() || nextIt->count > newCount)      nextIt = nodeList.insert(nextIt, {newCount, {key}});    else  // Node with count + 1 exists.      nextIt->keys.insert(key);     keyToIterator[key] = nextIt;    remove(listIt, key);  }   // Decrements the count of the key by 1.  void decrementExistingKey(      unordered_map<string, list<Node>::iterator>::iterator it,      const string& key) {    const auto listIt = it->second;    if (listIt->count == 1) {      keyToIterator.erase(it);      remove(listIt, key);      return;    }     auto prevIt = prev(listIt);    const int newCount = listIt->count - 1;    if (listIt == nodeList.begin() || prevIt->count < newCount)      prevIt = nodeList.insert(listIt, {newCount, {key}});    else  // Node with count - 1 exists.      prevIt->keys.insert(key);     keyToIterator[key] = prevIt;    remove(listIt, key);  }   // Removes the key from the node list.  void remove(list<Node>::iterator it, const string& key) {    it->keys.erase(key);    if (it->keys.empty())      nodeList.erase(it);  }}; 

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 ↗