✅ Design a Number Container System | JS🔥 | beats 99%🚀 | 🚀highly optimised & easy to understand ✅
Intuition
Data Structures:

indexToNumber: A Map to store the mapping from index to the number at that index.
numberToIndices: A Map to store the mapping from n
# Intuition
> **Data Structures:**
- indexToNumber: A Map to store the mapping from index to the number at that index.
- numberToIndices: A Map to store the mapping from number to a MinHeap of indices.
- MinHeap: A custom class that maintains a min-heap of indices using an array and a Map (indexMap) to efficiently track the position of each element within the heap.
# Approach
> **For change(index, number) method:**

**Remove old mapping:**
- If the index already has a number associated with it, retrieve the old number.
- If the old number exists in the numberToIndices map:
- Remove the index from the MinHeap associated with the old number using the delete method of the MinHeap class.
- If the MinHeap for the old number becomes empty, remove the old number from the numberToIndices map.

**Update mappings:**
- Store the new number at the given index in the indexToNumber map.
- If the number doesn't exist in the numberToIndices map, create a new MinHeap for it.
- Insert the index into the MinHeap associated with the given number.

> **For find(number) method:**
- If the number is not found in the numberToIndices map, return -1.
- Otherwise, use the peek() method of the MinHeap to return the minimum index without removing it from the heap.

> **MinHeap Class:**
- **insert(value):** Inserts a new value into the min-heap and maintains the heap property by bubbleUp-ing the newly inserted element.
- **extractMin():** Removes and returns the minimum value from the heap, maintaining the heap property by heapifyDown-ing the root element.
- **delete(value):** Removes a specific value from the heap by swapping it with the last element and then performing both heapifyDown and bubbleUp to restore the heap property.
- **peek():** Returns the minimum value (root of the heap) without removing it.
- **size():** Returns the number of elements in the heap.
- **Internal methods:** swap, parent, left, right, bubbleUp, heapifyDown for internal heap operations.

# Complexity
> **Time complexity:**

**for change(index, number):**
- Removing old mapping: O(log n) in the worst case due to MinHeap.delete().
- Inserting new mapping: O(log n) due to MinHeap.insert().
- Overall: O(log n)

**for find(number):**
- O(1) as MinHeap.peek() provides constant-time access to the minimum element.
> **Space complexity:**
- O(n) for indexToNumber and numberToIndices maps.
- O(n) in the worst case for the MinHeaps, where n is the number of unique numbers.
# Code
```javascript []
class NumberContainers {
    constructor() {
        this.indexToNumber = new Map();
        this.numberToIndices = new Map(); 
    }

    change(index, number) {
        if (this.indexToNumber.has(index)) {
            const oldNumber = this.indexToNumber.get(index);
            const oldIndices = this.numberToIndices.get(oldNumber);
            if (oldIndices) {
                oldIndices.delete(index); 
                if (oldIndices.size === 0) {
                    this.numberToIndices.delete(oldNumber);
                }
            }
        }

        this.indexToNumber.set(index, number);
        if (!this.numberToIndices.has(number)) {
            this.numberToIndices.set(number, new MinHeap()); 
        }
        this.numberToIndices.get(number).insert(index);
    }

    find(number) {
        if (!this.numberToIndices.has(number)) {
            return -1;
        }

        const minHeap = this.numberToIndices.get(number);
        return minHeap.size() > 0 ? minHeap.peek() : -1; 
    }
}

class MinHeap {
    constructor() {
        this.heap = [];
        this.indexMap = new Map(); 
    }

    insert(value) {
        this.heap.push(value);
        this.indexMap.set(value, this.heap.length - 1); 
        this.bubbleUp(this.heap.length - 1);
    }

    extractMin() {
        if (this.heap.length === 0) {
            return null;
        }

        const min = this.heap[0];
        this.swap(0, this.heap.length - 1);
        this.indexMap.delete(min); 
        this.heap.pop();
        this.heapifyDown(0);
        return min;
    }

    peek() {
        if (this.heap.length === 0) {
            return null;
        }
        return this.heap[0]; 
    }

    delete(value) {
        const index = this.indexMap.get(value);
        if (index === undefined) {
            return; // Element not found in the heap
        }

        this.swap(index, this.heap.length - 1);
        this.indexMap.delete(value); 
        this.heap.pop();

        this.heapifyDown(index); 
        this.bubbleUp(index); 
    }

    size() {
        return this.heap.length;
    }

    swap(i, j) {
        [this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
        this.indexMap.set(this.heap[i], i);
        this.indexMap.set(this.heap[j], j);
    }

    parent(i) {
        return Math.floor((i - 1) / 2);
    }

    left(i) {
        return 2 * i + 1;
    }

    right(i) {
        return 2 * i + 2;
    }

    bubbleUp(i) {
        while (i > 0 && this.heap[i] < this.heap[this.parent(i)]) {
            this.swap(i, this.parent(i));
            i = this.parent(i);
        }
    }

    heapifyDown(i) {
        let smallest = i;
        const left = this.left(i);
        const right = this.right(i);

        if (left < this.heap.length && this.heap[left] < this.heap[smallest]) {
            smallest = left;
        }

        if (right < this.heap.length && this.heap[right] < this.heap[smallest]) {
            smallest = right;
        }

        if (smallest !== i) {
            this.swap(i, smallest);
            this.heapifyDown(smallest);
        }
    }
}
```

![DALL·E 2025-01-21 10.20.30 - A realistic, soft-toned image of a kind and humble person, sitting at a desk with a gentle smile, holding a sign that says_ 'Please upvote if this hel.webp](https://assets.leetcode.com/users/images/ea47bfb2-ad93-4141-9856-6b4cb6152f25_1738982333.9529448.webp)

['JavaScript']