🚀 Beats 99.06% | HashMaps💡 | Lazy Deletion | Easiest Solution! | Solution for LeetCode-2349
IntuitionThe solution involves keeping track of the current number at each index and maintaining a priority queue (min-heap) for each number to efficiently find
![Screenshot 2025-02-08 141651.png](https://assets.leetcode.com/users/images/9d8d6a54-d35e-44cd-bbf1-9a1ce767ff0c_1739004435.5728083.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The solution involves keeping track of the current number at each index and maintaining a priority queue (min-heap) for each number to efficiently find the minimum index at which the number is present. This way, you can quickly update the number at an index and find the smallest index for a given number.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Data Structures:

    - currNumb: An unordered map that keeps track of the current number at each index.

        - Key: index (int) - the index of the container.

        - Value: number (int) - the current number stored at this index.

    - indexes: An unordered map that maintains a min-heap of indices for each number.

        - Key: number (int) - the number stored in the containers.

        - Value: minHeap (priority queue) - a min-heap of indices where the key number is stored, sorted in ascending order.

1. Methods:

    - Constructor: Initializes the NumberContainers object. Nothing specific needs to be initialized here since we're using unordered maps that are automatically initialized when accessed.

    - change(int index, int number): Updates the number at the given index and maintains the mapping in the min-heap.

        - currNumb[index] = number;: This line updates the currNumb map with the new number at the given index.

        - indexes[number].push(index);: This line adds the index to the priority queue (min-heap) for the given number in the indexes map.

    - find(int number): Returns the smallest index at which the given number is present. If the number is not present, returns -1.

        - while(!indexes[number].empty()): This loop runs while there are indices in the min-heap for the given number.

        - int currIdx = indexes[number].top();: This line gets the smallest index from the min-heap.

        - if(currNumb[currIdx] == number) return currIdx;: If the number at currIdx is still the given number, it returns currIdx.

        - indexes[number].pop();: If the number at currIdx is no longer the given number (it was changed), this line removes currIdx from the min-heap.

1. Algorithm:

    - When change(int index, int number) is called, the current number at the given index is updated, and the index is added to the min-heap for the new number.

    - When find(int number) is called, the algorithm retrieves the smallest index for the given number from the min-heap, ensuring that outdated indices (those where the number has changed) are removed.
# Complexity
- Time complexity: O(logn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->



# Code
```cpp []
class NumberContainers {
public:
    typedef priority_queue<int, vector<int>, greater<int>> minHeap;
    unordered_map<int,int> currNumb;        // mp[index] = currNumb
    unordered_map<int,minHeap> indexes;    // mp[number] = {index1,index2,index3}  --> index1<index2<index3
    NumberContainers() {
        
    }
    
    void change(int index, int number) {
        currNumb[index] = number;   // Updating the Given index with new Number.
        indexes[number].push(index);   // Adding the Given index in indexes of new Number.
    }
    
    int find(int number) {
        // If Number is Present then return its Minimum Index from MinHeap.
        while(!indexes[number].empty()){
            int currIdx = indexes[number].top();
            
            // If Number is still Present at currIdx then return currIndex.
            if(currNumb[currIdx] == number)
                return currIdx;
            // Else remove the currIdx from the minHeap of indexes of Given Number.
            indexes[number].pop();
        }
        // Else Return -1;
        return -1;
    }
};

```
['Hash Table', 'Design', 'Heap (Priority Queue)', 'Ordered Set', 'C++']