[LeetCode]#1656. Design an Ordered Stream

  • OrderedStream(int n) Constructs the stream to take n values.
  • String[] insert(int id, String value) Inserts the pair (id, value) into the stream, then returns the largest possible chunk of currently inserted values that appear next in the order.
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:

I am not so good as below code. I output every step and try analysis them.

  1. Default data and ptr as below.
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
Fatboy Slim

Fatboy Slim

Interesting in any computer science.