Python Elegant & Short | Amortized O(1) | Commented
\nclass LUPrefix:\n    """\n    Memory: O(n)\n    Time:   O(1) per upload call, because adding to the set takes O(1) time, and the prefix\n\t\t\t\t can be incre
```\nclass LUPrefix:\n    """\n    Memory: O(n)\n    Time:   O(1) per upload call, because adding to the set takes O(1) time, and the prefix\n\t\t\t\t can be increased no more than n times for all n calls to the upload function\n    """\n\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        # Since the prefix cannot decrease, it is enough for us to increase it\n        # until we reach the number that has not yet 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\nIf you like this solution remember to **upvote it** to let me know.\n
['Python', 'Python3']