Problem solution · Java

All O`one Data Structure

All O`one Data Structure: a Java 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
100 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

  • 100 lines of Java from the credited upstream file 432.java.
  • The implementation visibly relies on hash lookup, ordered 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 · JavaJava
Use this to learn the idea, then write your own version.
class Node {  public int count;  public Set<String> keys = new HashSet<>();  public Node prev = null;  public Node next = null;  public Node(int count) {    this.count = count;  }  public Node(int count, String key) {    this.count = count;    keys.add(key);  }} class AllOne {  public AllOne() {    head.next = tail;    tail.prev = head;  }   public void inc(String key) {    if (keyToNode.containsKey(key))      incrementExistingKey(key);    else      addNewKey(key);  }   public void dec(String key) {    // It is guaranteed that key exists in the data structure before the    // decrement.    decrementExistingKey(key);  }   public String getMaxKey() {    return tail.prev == head ? "" : tail.prev.keys.iterator().next();  }   public String getMinKey() {    return head.next == tail ? "" : head.next.keys.iterator().next();  }   private Map<String, Node> keyToNode = new HashMap<>();  private Node head = new Node(0);  private Node tail = new Node(0);   // Adds a new node with frequency 1.  private void addNewKey(final String key) {    if (head.next.count == 1)      head.next.keys.add(key);    else      insertAfter(head, new Node(1, key));    keyToNode.put(key, head.next);  }   // Increments the frequency of the key by 1.  private void incrementExistingKey(final String key) {    Node node = keyToNode.get(key);    node.keys.remove(key);     if (node.next == tail || node.next.count > node.count + 1)      insertAfter(node, new Node(node.count + 1));     node.next.keys.add(key);    keyToNode.put(key, node.next);     if (node.keys.isEmpty())      remove(node);  }   // Decrements the count of the key by 1.  private void decrementExistingKey(final String key) {    Node node = keyToNode.get(key);    node.keys.remove(key);     if (node.count > 1) {      if (node.prev == head || node.prev.count != node.count - 1)        insertAfter(node.prev, new Node(node.count - 1));      node.prev.keys.add(key);      keyToNode.put(key, node.prev);    } else {      keyToNode.remove(key);    }     if (node.keys.isEmpty())      remove(node);  }   private void insertAfter(Node node, Node newNode) {    newNode.prev = node;    newNode.next = node.next;    node.next.prev = newNode;    node.next = newNode;  }   private void remove(Node node) {    node.prev.next = node.next;    node.next.prev = node.prev;  }} 

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 ↗