[LeetCode]#1656. Design an Ordered Stream

Input
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
Output
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]

Analysis:

class OrderedStream:
def __init__(self, n: int):
self.data = [None] * (n + 1)
self.ptr = 1

def insert(self, id: int, value: str):
self.data[id] = value
if id == self.ptr:
while self.ptr < len(self.data) and self.data[self.ptr]:
self.ptr += 1
return self.data[id:self.ptr]
return []

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store