Python | HashMap + Heap
For each number we have a set and a heap of indexes. The set lets us check quick if number is at index or not. The heap lets us return quick what is the smalles
For each `number` we have a set and a heap of indexes. The set lets us check quick if `number` is at `index` or not. The heap lets us return quick what is the smallest `index` at which `number` is.\n\n```\nclass NumberContainers:\n    def __init__(self):\n        self.numbersByIndex = {}\n        self.numberIndexes = defaultdict(set)\n        self.numberIndexesHeap = defaultdict(list)\n\n    def change(self, index: int, number: int) -> None:\n        if index in self.numbersByIndex:\n            if number != self.numbersByIndex[index]:\n                self.numberIndexes[self.numbersByIndex[index]].remove(index)\n                self.numbersByIndex[index] = number\n                self.numberIndexes[number].add(index)\n                heappush(self.numberIndexesHeap[number], index)\n        else:\n            self.numbersByIndex[index] = number\n            self.numberIndexes[number].add(index)\n            heappush(self.numberIndexesHeap[number], index)\n\n    def find(self, number: int) -> int:\n        while self.numberIndexesHeap[number] and self.numberIndexesHeap[number][0] not in self.numberIndexes[number]:\n                heappop(self.numberIndexesHeap[number])  # make sure the smallest index in heap is still an index for number\n        return self.numberIndexesHeap[number][0] if self.numberIndexesHeap[number] else -1\n\n```
['Heap (Priority Queue)', 'Python']