> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/weekly-contest/432.-all-o-one-data-structure.md).

# 432. All O\`one Data Structure

```
T: O(n + clogc)
S: O(n + c)
```

````java
```java
class AllOne {

    Map<String, Integer> counterMap;
    TreeMap<Integer, Set<String>> sortedCountMap;

    public AllOne() {
        this.counterMap = new HashMap<>();
        this.sortedCountMap = new TreeMap<>();
    }
    
    public void inc(String key) {
        if (counterMap.containsKey(key)) {
            int oldCount = counterMap.get(key);
            removeSortedCountMap(oldCount, key);
        }

        int newCount = counterMap.getOrDefault(key, 0) + 1; // hello, 2
        counterMap.put(key, newCount);

        addSortedCountMap(newCount, key); // 2, [hello]
    }
    
    public void dec(String key) {
        int oldCount = counterMap.get(key);
        removeSortedCountMap(oldCount, key);

        int newCount = oldCount - 1;
        counterMap.put(key, newCount);
        
        if (newCount != 0) {
            addSortedCountMap(newCount, key);
        } else {
            counterMap.remove(key);
        }

    }

    private void removeSortedCountMap(int oldCount, String key) {
        Set<String> keys = sortedCountMap.get(oldCount);
        keys.remove(key);

        if (keys.isEmpty()) {
            sortedCountMap.remove(oldCount);
        }
    }

    private void addSortedCountMap(int newCount, String key) {
        sortedCountMap.putIfAbsent(newCount, new HashSet<>());
        sortedCountMap.get(newCount).add(key);
    }
    
    public String getMaxKey() {
        String result = "";
        if (sortedCountMap.size() == 0) {
            return "";
        }

        Integer lastKey = sortedCountMap.lastKey();
        if (lastKey == null) {
            return "";
        }
        Set<String> keys = sortedCountMap.get(lastKey);
        if (keys.isEmpty()) {
            return "";
        }
        for (String key : keys) {
            result = key;
            break;
        }
        return result;
    }
    
    public String getMinKey() {
        String result = "";
        if (sortedCountMap.size() == 0) {
            return "";
        }

        Integer firstKey = sortedCountMap.firstKey();
        if (firstKey == null) {
            return "";
        }
        Set<String> keys = sortedCountMap.get(firstKey);
        if (keys.isEmpty()) {
            return "";
        }
        for (String key : keys) {
            result = key;
            break;
        }
        return result;
    }
}

/**
 * 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();

T: O(n + clogc)
S: O(n + c)

key, count

when update count, find old count first, remove from set

update new count and set
if count is 0, no need update

add to another one
count, Set<key>

getLastKey()
getFirstKey()


a 1
b 2

 */
```
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/weekly-contest/432.-all-o-one-data-structure.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
