"Python Concise and Efficient | Achieving Amortized O(1) Complexity | Annotated for Clarity"
Intuition & Approach :\nThe class LUPrefix is designed to efficiently track the longest prefix of consecutive integers. It uses a set _nums to store uploaded vi
# Intuition & Approach :\nThe class LUPrefix is designed to efficiently track the longest prefix of consecutive integers. It uses a set _nums to store uploaded videos, and the variable _longest to maintain the length of the longest consecutive prefix.\n\nIn the constructor __init__, it initializes _longest to 0 and creates an empty set _nums.\n\nIn the upload method, when a new video is uploaded, it\'s added to the set. Then, it checks if there\'s a consecutive video in the set that can extend the current prefix. It does this by continuously incrementing _longest until it reaches a number that has not been uploaded. This ensures that the longest prefix is always up to date.\n\nThe longest method simply returns the current value of _longest, representing the length of the longest consecutive prefix.\n\n# Complexity\n- Time: O(1) for init and longest, O(1) amortized for upload. \n- Space: O(n) for distinct videos. \n- Efficiently tracks longest prefix.\n\n# Code\n```\nclass LUPrefix:\n    def __init__(self, n: int):\n        self._longest = 0\n        self._nums = set()\n\n    def upload(self, video: int) -> None:\n        self._nums.add(video)\n        # Increase the prefix until reaching a number that has not been added\n        while self._longest + 1 in self._nums:\n            self._longest += 1\n\n    def longest(self) -> int:\n        return self._longest\n\n\n```
['Python3']