Python SortedList and Dictionary | Easy to Understand
We keep a mapping from integer to numbers and numbers to sorted indices.\n\nDuring update:\n We check if the number has been seen before and if it\'s changing i
We keep a mapping from integer to numbers and numbers to sorted indices.\n\nDuring update:\n* We check if the number has been seen before and if it\'s changing it\'s index. If it is, handle that case.\n* If the number has never been seen before, initialize the SortedList containing the indices\n* If the number has been seen before, clear the index as not to create duplicates.\n* Add the number to both the mappings.\n\n```\nfrom sortedcontainers import SortedList\n\nclass NumberContainers:\n\n    def __init__(self):\n        self.n2i = {}\n        self.i2n = {}\n\n    def change(self, index: int, number: int) -> None:\n        if index in self.i2n and self.i2n[index] != number:\n            orig_num = self.i2n[index]\n            self.n2i[orig_num].discard(index)\n            \n        if number not in self.n2i:\n            self.n2i[number] = SortedList()\n        else:\n            self.n2i[number].discard(index)\n            \n        self.i2n[index] = number\n        self.n2i[number].add(index)\n        #print(\'change\', index, number, self.n2i[number])\n\n    def find(self, number: int) -> int:\n        if number in self.n2i:\n            if len(self.n2i[number]) > 0:\n                return self.n2i[number][0]\n            else:\n                return -1\n        else:\n            return -1```\n\t\t\t
['Python']