1656. Design an Ordered Stream
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 82% Topics: Array, Hash Table, Design, Data Stream
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(1), amortized
# Space: O(n)
class OrderedStream(object):
def __init__(self, n):
"""
:type n: int
"""
self.__i = 0
self.__values = [None]*n
def insert(self, id, value):
"""
:type id: int
:type value: str
:rtype: List[str]
"""
id -= 1
self.__values[id] = value
result = []
if self.__i != id:
return result
while self.__i < len(self.__values) and self.__values[self.__i]:
result.append(self.__values[self.__i])
self.__i += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions