question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
design-an-ordered-stream
bad problem description
bad-problem-description-by-thelotuseater-5rhs
What is the problem asking for, the description and the example are so conflicting. English is my first language, I have never had any issues with understanding
thelotuseater
NORMAL
2022-08-05T01:59:14.979103+00:00
2022-08-05T02:00:43.070965+00:00
7,812
false
What is the problem asking for, the description and the example are so conflicting. English is my first language, I have never had any issues with understanding problems on LC. I am a premium user, please fix this.
161
0
[]
13
design-an-ordered-stream
Java Straightforward Solution!
java-straightforward-solution-by-admin00-r6ow
Well, the problem statement is not very clear even there is a GIF, although I work it out. The problem statement makes it feel as a medium problem, I guess. \n\
admin007
NORMAL
2020-11-15T04:01:26.480013+00:00
2020-11-15T07:34:11.551157+00:00
14,740
false
Well, the problem statement is not very clear even there is a GIF, although I work it out. The problem statement makes it feel as a medium problem, I guess. \n\nBasically, the idea is that you need to return a longest list that start at index of ptr. if ptr is not pointing an element, you need to return a empty list.\n```\nclass OrderedStream {\n int ptr;\n String[] res;\n \n public OrderedStream(int n) {\n ptr = 0;\n res = new String[n];\n }\n \n public List<String> insert(int id, String value) {\n List<String> list = new ArrayList<>();\n \n res[id - 1] = value;\n while (ptr < res.length && res[ptr] != null) {\n list.add(res[ptr]);\n ptr++;\n }\n \n return list;\n }\n}\n```
72
2
['Java']
8
design-an-ordered-stream
[Python3] simple solution
python3-simple-solution-by-ye15-2k67
\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.data = [None]*n\n self.ptr = 0 # 0-indexed \n\n def insert(self, id: int, va
ye15
NORMAL
2020-11-15T04:03:14.174746+00:00
2020-11-15T04:03:14.174782+00:00
14,274
false
\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.data = [None]*n\n self.ptr = 0 # 0-indexed \n\n def insert(self, id: int, value: str) -> List[str]:\n id -= 1 # 0-indexed \n self.data[id] = value \n if id > self.ptr: return [] # not reaching ptr \n \n while self.ptr < len(self.data) and self.data[self.ptr]: self.ptr += 1 # update self.ptr \n return self.data[id:self.ptr]\n```
60
1
['Python3']
4
design-an-ordered-stream
Bloomberg Question with Variation
bloomberg-question-with-variation-by-vin-ij41
I recently got this quesiton in a phone interview at Bloomberg with a minor variation. The variation was that you won\'t be given size in advance. So, try to so
vinodghai713
NORMAL
2022-10-05T21:06:49.567256+00:00
2022-10-05T21:06:49.567298+00:00
4,400
false
I recently got this quesiton in a phone interview at Bloomberg with a minor variation. The variation was that you won\'t be given **size** in advance. So, try to solve this question without using variable **n** in the constructor. Here is my implementation.\n\n```\nclass OrderedStream {\n private Map<Integer, String> buffer;\n private int currentPointer;\n public OrderedStream(int n) {\n currentPointer = 1;\n buffer = new HashMap<>();\n }\n \n public List<String> insert(int idKey, String value) {\n List<String> values = new ArrayList<>();\n if (idKey == currentPointer) {\n values.add(value);\n while (buffer.containsKey(++currentPointer))\n values.add(buffer.remove(currentPointer));\n } else\n buffer.put(idKey, value);\n return values;\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = new OrderedStream(n);\n * List<String> param_1 = obj.insert(idKey,value);\n */\n```
33
0
['Java']
5
design-an-ordered-stream
Latest Python solution explained for newly updated description: Faster than 90%
latest-python-solution-explained-for-new-vgrx
The idea behind this problem is simple but the description is confusing.\nSo let me explain a bit.\n\nBasically , we need to store every incoming value at the g
er1shivam
NORMAL
2021-07-14T03:04:33.554067+00:00
2021-07-14T03:04:33.554113+00:00
4,502
false
The idea behind this problem is simple but the description is confusing.\nSo let me explain a bit.\n\nBasically , we need to store every incoming value at the given index. And \nwith every incoming index, we have to check\n\n* If the current index is less than the incoming index, the we have to return\n an empty list\n\n* Else , we have to return an sliced list from the incoming index to the first index\n where there is no insertion till yet.\n\nSolution:\n* Initialize a list of size n with None\n* Maintain the current index with self.ptr\n* For every insert call, with idKey, value \n * Assign the list[idKey-1] to the value # Since array is 0-index reduce 1\n * Check if the current index is less than incoming index(idKey-1) and return []\n * Else return sliced list from incoming index(idKey-1) till we do not encounter None.\n\n\n```\nclass OrderedStream:\n\n def __init__(self, n):\n self.stream = [None]*n\n self.ptr = 0\n\n def insert(self, idKey, value):\n idKey -= 1\n self.stream[idKey] = value\n if self.ptr < idKey:\n return []\n else:\n while self.ptr < len(self.stream) and self.stream[self.ptr] is not None:\n self.ptr += 1\n return self.stream[idKey:self.ptr]\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```
33
0
['Python', 'Python3']
2
design-an-ordered-stream
[JavaScript] Elegant solution using an array and pointer (with explanation)
javascript-elegant-solution-using-an-arr-1yvf
This is a rather simple problem, all we need to do is to store all the data that was passed to insert() function, return some data if the condition is true, and
mrhyde
NORMAL
2021-02-22T22:45:48.229029+00:00
2021-06-22T23:25:32.275889+00:00
2,556
false
This is a rather simple problem, all we need to do is to store all the data that was passed to `insert()` function, return some data if the condition is true, and keep track of where we were last time.\n\nLook how clean this solution is:\n```javascript\nclass OrderedStream {\n constructor(n) {\n this.pointer = 0\n this.list = []\n }\n\n insert(id, value) {\n let chunk = []\n this.list[id - 1] = value\n while(this.list[this.pointer]) {\n chunk.push(this.list[this.pointer])\n this.pointer++\n }\n return chunk\n }\n}\n```\n\nWith explanation:\n```javascript\nclass OrderedStream {\n // Define a construction function and set some values as object properties to keep our data persistent between invocations\n constructor(n) {\n this.pointer = 0\n // this will create an array of length (n) and set all values to \'undefined\'\n this.list = []\n }\n\n insert(id, value) {\n // will be used to store values that pass the condition and to be returned\n let chunk = []\n // since array indices start from zero and id in this problem from 1 we need to decrement it\n this.list[id - 1] = value\n // every time we insert a value we have to look if there is a value at the index (pointer) that should be returned\n // if there is any we copy it and then iterate to the next element until the condition is no longer true\n while(this.list[this.pointer]) {\n chunk.push(this.list[this.pointer])\n this.pointer++\n }\n return chunk\n }\n}\n```
26
1
['JavaScript']
2
design-an-ordered-stream
C++ Array
c-array-by-votrubac-wb74
cpp\nvector<string> s;\nint ptr = 1;\nOrderedStream(int n) : s(n + 1) {}\nvector<string> insert(int id, string value) {\n s[id] = value;\n vector<string>
votrubac
NORMAL
2020-11-15T05:00:47.505906+00:00
2021-02-18T10:47:47.655621+00:00
4,567
false
```cpp\nvector<string> s;\nint ptr = 1;\nOrderedStream(int n) : s(n + 1) {}\nvector<string> insert(int id, string value) {\n s[id] = value;\n vector<string> res;\n while (ptr < s.size() && !s[ptr].empty())\n res.push_back(s[ptr++]);\n return res;\n}\n```
24
2
[]
3
design-an-ordered-stream
Python using dict, 212ms 14.6MB
python-using-dict-212ms-146mb-by-vashik-dom3
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.seen = {}\n self.ptr = 1\n\n def insert(self, id: int, value: str) -> List[str
vashik
NORMAL
2020-11-16T13:47:52.104013+00:00
2020-11-16T13:47:52.104045+00:00
2,146
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.seen = {}\n self.ptr = 1\n\n def insert(self, id: int, value: str) -> List[str]:\n seen, ptr = self.seen, self.ptr\n \n seen[id] = value\n result = []\n while ptr in seen:\n result.append(seen[ptr])\n del seen[ptr]\n ptr += 1\n \n self.ptr = ptr\n return result\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(id,value)\n```
17
0
['Python', 'Python3']
2
design-an-ordered-stream
C++ Self Explainatory
c-self-explainatory-by-0xsupra-g290
\nclass OrderedStream {\npublic:\n \n vector<string>res;\n int ptr;\n OrderedStream(int n) {\n res.resize(n);\n ptr = 1;\n }\n \
0xsupra
NORMAL
2020-11-15T04:01:36.290465+00:00
2020-11-15T04:01:36.290512+00:00
4,003
false
```\nclass OrderedStream {\npublic:\n \n vector<string>res;\n int ptr;\n OrderedStream(int n) {\n res.resize(n);\n ptr = 1;\n }\n \n vector<string> insert(int id, string value) {\n res[id-1] = value;\n vector<string> ans;\n \n if(ptr == id) {\n int i = ptr - 1;\n \n for(; i < res.size(); i++) {\n if(res[i] == "")\n break;\n ans.push_back(res[i]);\n }\n ptr = i+1;\n }\n return ans;\n }\n};\n```
13
1
['C', 'C++']
0
design-an-ordered-stream
Python3 Solution + Better Description Because This One Is Really Bad
python3-solution-better-description-beca-lv4j
The description as of June 13th, 2022 mentions nothing a ptr. There is a ptr that should be initialized at 1. If an item is inserted with an idKey above the ptr
bretticus-mc
NORMAL
2022-06-14T00:30:22.773100+00:00
2022-06-14T00:30:22.773127+00:00
1,345
false
The description as of June 13th, 2022 mentions nothing a ptr. There is a ptr that should be initialized at 1. If an item is inserted with an idKey above the ptr, return nothing. If an item is inserted that matches the ptr, return the largest chunk of contiguous values above the ptr. The ptr should update to the index immediately after the returned chunk. \n\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.ptr = 1\n self.hashmap = dict()\n \n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.hashmap[idKey] = value\n output = []\n if idKey > self.ptr:\n return output\n \n while idKey in self.hashmap:\n output.append(self.hashmap[idKey])\n idKey += 1\n self.ptr = idKey\n \n return output\n```
12
1
['Python3']
0
design-an-ordered-stream
C++ God Solution
c-god-solution-by-vishal_k78-fyhk
Map STL\n\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int maxSize = -1,currIndex = 1;\n \n OrderedStream(int n) {\n maxSize = n;\
vishal_k78
NORMAL
2022-10-30T09:43:40.608402+00:00
2022-10-30T09:43:40.608428+00:00
1,761
false
**Map STL**\n```\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int maxSize = -1,currIndex = 1;\n \n OrderedStream(int n) {\n maxSize = n;\n for(int i=1;i<=n;i++) mp[i] = "----";\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> res;\n mp[idKey] = value;\n int i =1;\n for(int i=currIndex;i<=maxSize;i++){\n if(mp[i] != "----"){\n res.push_back(mp[i]);\n }else{\n currIndex = i;\n break;\n }\n }\n \n return res;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```\n<br>\n<div> Happy coding </div>\nPlease do upvote this post.\n\n![image](https://assets.leetcode.com/users/images/5ce11bf6-0db9-4239-bbb9-dfe5d76443f3_1666346629.9681695.gif)
10
1
['Array', 'C', 'C++']
2
design-an-ordered-stream
Java, faster than 100%. Understand the requirments is much harder than problem itself
java-faster-than-100-understand-the-requ-r94b
\nclass OrderedStream {\n \n private String[] values;\n private int ptr;\n\n public OrderedStream(int n) {\n values = new String[n];\n
yurokusa
NORMAL
2020-11-16T21:22:37.916228+00:00
2020-11-16T21:23:29.827822+00:00
2,871
false
```\nclass OrderedStream {\n \n private String[] values;\n private int ptr;\n\n public OrderedStream(int n) {\n values = new String[n];\n ptr = 0;\n }\n \n public List<String> insert(int id, String value) {\n values[id - 1] = value;\n\n List<String> result = new ArrayList();\n while (ptr < values.length && values[ptr] != null) {\n result.add(values[ptr++]);\n }\n\n return result;\n }\n}\n```
10
0
['Java']
5
design-an-ordered-stream
C++ easiest solutin
c-easiest-solutin-by-aparna_g-muhj
\nclass OrderedStream {\npublic:\n vector<string> stream ; //to get input\n int i=0;\n OrderedStream(int n) {\n stream.resize(n); //change the s
aparna_g
NORMAL
2020-11-21T13:36:32.870581+00:00
2020-11-21T13:36:32.870609+00:00
1,632
false
```\nclass OrderedStream {\npublic:\n vector<string> stream ; //to get input\n int i=0;\n OrderedStream(int n) {\n stream.resize(n); //change the size of the vector when orderedStream is called\n }\n \n vector<string> insert(int id, string value) {\n vector<string> result;\n stream[id-1] = value; // value is stored in the ith position\n while(i<stream.size() && stream[i]!="") //stream[i] !="" because an entry is printed only if the entry before it is printed\n {\n result.push_back(stream[i]);\n i++;\n }\n return result;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(id,value);\n */\n```
8
1
['C', 'C++']
0
design-an-ordered-stream
C++||Easy to Understand
ceasy-to-understand-by-return_7-vent
```\nclass OrderedStream {\npublic:\n vector stream;\n int i=0;\n OrderedStream(int n) \n {\n stream.resize(n);\n \n }\n \n v
return_7
NORMAL
2022-09-08T14:50:58.363757+00:00
2022-09-08T14:50:58.363805+00:00
1,134
false
```\nclass OrderedStream {\npublic:\n vector<string> stream;\n int i=0;\n OrderedStream(int n) \n {\n stream.resize(n);\n \n }\n \n vector<string> insert(int idKey, string value)\n {\n vector<string> res;\n stream[idKey-1]=value;\n while(i<stream.size()&&stream[i]!="")\n {\n res.push_back(stream[i]);\n i++;\n }\n return res;\n \n }\n};\n//if you like the solution plz upvote.
7
0
['C']
1
design-an-ordered-stream
super clear 🐍 illustrated explanation
super-clear-illustrated-explanation-by-w-mtkk
<-- please vote\n\n\n\n\nclass OrderedStream:\n\n def init(self, n: int):\n """ O(N)S"""\n self.arr = [None] * (n + 1)\n self.ptr = 1\n\
wilmerkrisp
NORMAL
2022-05-16T12:57:59.080598+00:00
2022-05-16T12:57:59.080626+00:00
612
false
<-- please vote\n\n![image](https://assets.leetcode.com/users/images/71bcfb09-61a6-46cb-9c06-189364872183_1652705869.6314733.png)\n\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n """ O(N)S"""\n self.arr = [None] * (n + 1)\n self.ptr = 1\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.arr[idKey] = value\n while self.ptr < len(self.arr) and self.arr[self.ptr] is not None:\n self.ptr += 1\n return self.arr[idKey:self.ptr]
7
0
[]
0
design-an-ordered-stream
Easiest C++ Code
easiest-c-code-by-baibhavsingh07-o8cn
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
baibhavsingh07
NORMAL
2023-05-31T12:51:37.468977+00:00
2023-05-31T12:51:37.469001+00:00
1,883
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass OrderedStream {\npublic:\n\n string v[1000];\n int curr=0;\n\n OrderedStream(int n) {\n\n\n \n }\n \n vector<string> insert(int idKey, string value) {\n v[idKey-1]=value;\n vector<string>ans;\n while(v[curr]!=""){\n ans.push_back(v[curr]);\n curr++;\n }\n return ans;\n\n \n \n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```
6
0
['Array', 'Design', 'Data Stream', 'C++']
1
design-an-ordered-stream
Python Easy to Understand Solution
python-easy-to-understand-solution-by-ra-gbet
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.val=[[] for i in range(n+1)]\n self.left=0\n\n def insert(self, id: int, value
ragav_1302
NORMAL
2020-11-15T06:05:44.557006+00:00
2020-11-15T06:05:44.557042+00:00
865
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.val=[[] for i in range(n+1)]\n self.left=0\n\n def insert(self, id: int, value: str) -> List[str]:\n self.val[id-1]=value\n if self.left!=id-1:\n return []\n ans=[]\n while self.val[self.left]!=[]:\n ans.append(self.val[self.left])\n self.left+=1 \n return ans\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(id,value)\n```
6
0
[]
1
design-an-ordered-stream
notmy_OrderedStream
notmy_orderedstream-by-rinatmambetov-p5zc
# Intuition \n\n\n\n\n\n\n\n\n\n\n\n# Code\n\nclass OrderedStream {\n\tconstructor(n) {\n\t\tthis.pointer = 0;\n\t\tthis.list = [];\n\t}\n\n\tinsert(id, val)
RinatMambetov
NORMAL
2023-05-09T15:34:33.612384+00:00
2023-05-09T15:34:33.612412+00:00
880
false
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity\n- Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass OrderedStream {\n\tconstructor(n) {\n\t\tthis.pointer = 0;\n\t\tthis.list = [];\n\t}\n\n\tinsert(id, val) {\n\t\tlet chunk = [];\n\t\tthis.list[id - 1] = val;\n\t\twhile (this.list[this.pointer]) {\n\t\t\tchunk.push(this.list[this.pointer]);\n\t\t\tthis.pointer++;\n\t\t}\n\t\treturn chunk;\n\t}\n}\n```
5
0
['JavaScript']
0
design-an-ordered-stream
C++ Easy Solution || Clean Code
c-easy-solution-clean-code-by-dee_stroye-g2b2
\nclass OrderedStream {\npublic:\n int pointer = 0;\n vector<string>stream;\n \n OrderedStream(int n) {\n stream.resize(n);\n }\n \n
dee_stroyer
NORMAL
2022-01-10T16:29:59.533413+00:00
2022-01-10T16:29:59.533454+00:00
871
false
```\nclass OrderedStream {\npublic:\n int pointer = 0;\n vector<string>stream;\n \n OrderedStream(int n) {\n stream.resize(n);\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string>ans;\n \n stream[idKey-1] = value;\n \n while(pointer<stream.size() and stream[pointer] != ""){\n ans.push_back(stream[pointer]);\n pointer++;\n }\n \n return ans;\n }\n};\n```
5
0
['C', 'C++']
1
design-an-ordered-stream
JavaScript Simple Array Solution - 98% Faster, 27% Less Memory
javascript-simple-array-solution-98-fast-mfrm
I didn\'t use a hash table, I just initialized an array with a pointer and when the pointer had data I push it into a result array and returned the result array
rachelsalazar
NORMAL
2021-08-19T23:34:44.491701+00:00
2021-08-19T23:42:42.606252+00:00
1,095
false
I didn\'t use a hash table, I just initialized an array with a pointer and when the pointer had data I push it into a result array and returned the result array. See below:\n```\n/**\n * @param {number} n\n */\nvar OrderedStream = function(n) {\n // initialize array\n this.arr = [];\n // start pointer at index of 0\n this.p = 0;\n //set array to have length of n\n this.arr.length = n;\n};\n\n/** \n * @param {number} idKey \n * @param {string} value\n * @return {string[]}\n */\nOrderedStream.prototype.insert = function(idKey, value) {\n // push the value into array at index of idKey\n this.arr[idKey - 1] = value;\n // initialize result array\n let result = [];\n \n // while the pointer has a value, push that value into the result array and advance the pointer\n while (this.arr[this.p]) {\n result.push(this.arr[this.p]);\n this.p++;\n }\n // return the result array which will either be empty if the pointer was null, or will have the chunks pushed into it\n return result;\n};\n\n/** \n * Your OrderedStream object will be instantiated and called as such:\n * var obj = new OrderedStream(n)\n * var param_1 = obj.insert(idKey,value)\n */\n ```
5
0
['JavaScript']
1
design-an-ordered-stream
Bad description (C++ : solved using animation in example)
bad-description-c-solved-using-animation-3267
\nclass OrderedStream {\npublic:\n vector<string> vec;\n int n, pointer;\n OrderedStream(int n) {\n vec.resize(n+1, "");\n this->n
mazhar_mik
NORMAL
2021-08-11T13:23:00.120532+00:00
2021-08-11T13:23:00.120561+00:00
331
false
```\nclass OrderedStream {\npublic:\n vector<string> vec;\n int n, pointer;\n OrderedStream(int n) {\n vec.resize(n+1, "");\n this->n = n;\n this->pointer = 1;\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> result;\n vec[idKey] = value;\n if(idKey == pointer) {\n while(pointer <= n && vec[pointer] != "") {\n result.push_back(vec[pointer]);\n pointer++;\n }\n }\n \n return result;\n }\n};\n```
5
0
[]
0
design-an-ordered-stream
C++ solution with explanation
c-solution-with-explanation-by-rmallela0-cmrv
\nclass OrderedStream {\nprivate:\n std::vector<string> stream;\n int readIdx;\n \npublic:\n OrderedStream(int n) {\n // resize the vector st
rmallela0426
NORMAL
2021-04-15T22:03:56.008283+00:00
2021-04-15T22:03:56.008312+00:00
397
false
```\nclass OrderedStream {\nprivate:\n std::vector<string> stream;\n int readIdx;\n \npublic:\n OrderedStream(int n) {\n // resize the vector stream\n stream.resize(n);\n // Initialize the readIdx to starting point\n readIdx = 0; \n }\n \n vector<string> insert(int idKey, string value) {\n // Insert the value at specified index\n stream[idKey-1] = value;\n \n // Current string at readidx is filled, need to return\n // the longest possible chunk. Loop all the values of\n // the stream starting from read idx and append it to\n // result only if it is not empty or reaches an index\n // where the string is empty\n int start = readIdx;\n while(readIdx < stream.size() && !stream[readIdx].empty()) {\n // String is inserted at this read Idx, increament the\n // readIdx and numelem\n readIdx++;\n }\n \n return vector<string>(stream.begin()+start, stream.begin()+readIdx);\n }\n};\n```
5
0
[]
0
design-an-ordered-stream
C++ Solution | Bloomberg High Frequency Problem
c-solution-bloomberg-high-frequency-prob-3v75
This is the Bloomberg High Frequency Problem. They asked the same question wrapping it as printing package of a UDP process. \n\n\nclass OrderedStream {\nprivat
vwo50
NORMAL
2021-03-14T22:18:35.220779+00:00
2021-03-14T22:18:35.220818+00:00
631
false
This is the Bloomberg High Frequency Problem. They asked the same question wrapping it as printing package of a UDP process. \n\n```\nclass OrderedStream {\nprivate:\n int id = 1; \n \n unordered_map<int, string> m;\npublic:\n OrderedStream(int n) {\n \n }\n \n vector<string> insert(int idKey, string value) {\n m[idKey] = value; vector<string> res;\n \n while (m.find(id) != m.end()) {\n res.emplace_back(m[id]); id++;\n }\n \n return res;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```
5
0
['C']
0
design-an-ordered-stream
Golang solution
golang-solution-by-sisigogo-ug30
\ntype OrderedStream struct {\n ptr int\n list []string\n}\n\nfunc Constructor(n int) OrderedStream {\n return OrderedStream{0,make([]string,n)}\n}\n\n
sisigogo
NORMAL
2020-11-19T18:19:43.317731+00:00
2020-11-19T18:19:43.317774+00:00
247
false
```\ntype OrderedStream struct {\n ptr int\n list []string\n}\n\nfunc Constructor(n int) OrderedStream {\n return OrderedStream{0,make([]string,n)}\n}\n\n\nfunc (this *OrderedStream) Insert(id int, value string) []string {\n this.list[id-1] = value\n if this.list[this.ptr] == "" {\n return []string{}\n } else {\n for i,v := range this.list[this.ptr:]{\n if v == "" {\n temp := this.ptr\n this.ptr += i\n return this.list[temp:this.ptr]\n }\n }\n }\n return this.list[this.ptr:]\n}\n```
4
0
['Go']
0
design-an-ordered-stream
C++ Solution || HashMaps || Arrays✔✔
c-solution-hashmaps-arrays-by-akanksha98-6zua
\n## MAPS\n\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int count=1;\n OrderedStream(int n) {\n count=1;\n }\n vector<string>
akanksha984
NORMAL
2023-05-06T12:32:32.476260+00:00
2023-05-06T13:02:28.175120+00:00
680
false
\n## MAPS\n```\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int count=1;\n OrderedStream(int n) {\n count=1;\n }\n vector<string> insert(int idKey, string value) {\n mp.insert({idKey,value});\n vector<string> ans;\n for (auto vl: mp){\n if (vl.first==count){\n count++;\n ans.push_back(vl.second);\n }\n }\n return ans;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```\n\n## ARRAYS\n```\nclass OrderedStream {\npublic:\n string arr[1005];\n int count=1; int n;\n OrderedStream(int n) {\n count=1;\n for (int i=1; i<=n; i++)arr[i]="NULLSTRING";\n this->n= n;\n }\n vector<string> insert(int idKey, string value) {\n arr[idKey]= value;\n vector<string> ans;\n while (count<=n && arr[count]!= "NULLSTRING"){\n ans.push_back(arr[count]);\n count++;\n }\n return ans;\n }\n};\n```
3
0
['Array', 'Hash Table', 'Design', 'Data Stream', 'C++']
2
design-an-ordered-stream
Python solution with a hint before the solution.
python-solution-with-a-hint-before-the-s-r5ys
Approach\n Describe your approach to solving the problem. \nIf you don\'t want to go through my code, I suggest you to watch the example video and observe the "
kush_ishere
NORMAL
2022-12-28T11:45:12.917984+00:00
2022-12-28T11:47:55.899291+00:00
1,869
false
# Approach\n<!-- Describe your approach to solving the problem. -->\nIf you don\'t want to go through my code, I suggest you to watch the example video and observe the "ptr" and how it is incrementing and returning things based on the ptr value.\n\n# Code\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.arr = [""] * n\n self.ptr = 0\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.arr[idKey-1] = value\n res = []\n if self.ptr == idKey - 1:\n while self.ptr < len(self.arr) and self.arr[self.ptr] != "":\n print(self.ptr)\n res.append(self.arr[self.ptr])\n self.ptr += 1\n\n return res\n\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```
3
0
['Python3']
0
design-an-ordered-stream
Java || O (n) || Explanation and code with comments
java-o-n-explanation-and-code-with-comme-vdrb
Even though the description may not be entirely clear, paying close attention to the attached GIF will help.\n\n1) A pointer starts at 0-th position (0-based)\n
kaushik_mitra
NORMAL
2022-06-28T19:45:07.064242+00:00
2022-06-28T19:45:07.064285+00:00
846
false
Even though the description may not be entirely clear, paying close attention to the attached GIF will help.\n\n1) A pointer starts at 0-th position (0-based)\n2) String data is inserted at K-th position (idKey minus 1)\n3) Try to move pointer all the way to right if locations are filled\n\n```\nclass OrderedStream {\n String[] values; //For index-wise data insertion\n int ptr = 0; //Starting position of pointer\n \n public OrderedStream(int n) {\n values = new String[n];\n }\n \n public List<String> insert(int idKey, String value) {\n List<String> output = new ArrayList();\n values[idKey - 1] = value;\n\t\t//While values[ptr] is filled, forward ptr\n while (ptr < values.length && values[ptr] != null) {\n output.add(values[ptr++]);\n }\n return output;\n }\n}\n```
3
0
['Java']
1
design-an-ordered-stream
Python: holding last min index
python-holding-last-min-index-by-ryabkin-223r
\nclass OrderedStream(object):\n\n def __init__(self, n):\n """\n :type n: int\n """\n self.values = [None] * n\n self.ret
ryabkin
NORMAL
2021-12-06T15:34:04.979803+00:00
2021-12-06T15:34:38.939530+00:00
478
false
```\nclass OrderedStream(object):\n\n def __init__(self, n):\n """\n :type n: int\n """\n self.values = [None] * n\n self.returned_min = 0\n \n\n def insert(self, idKey, value):\n """\n :type idKey: int\n :type value: str\n :rtype: List[str]\n """\n self.values[idKey-1] = value\n \n ret = []\n\t\t# Value not None and index in boundary of values array size\n while self.returned_min < len(self.values) and self.values[self.returned_min]:\n ret.append(self.values[self.returned_min])\n\t\t\t# Holding last min index returned\n self.returned_min += 1\n \n return ret\n \n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```\n\nThe idea is that None value holes are skipped until filled, min boundary of the last returned non-None value is kept in `returned_min` variable
3
0
['Python']
0
design-an-ordered-stream
[JavaScript] Easy solution
javascript-easy-solution-by-joviho-ygru
```\n/\n * @param {number} n\n */\nvar OrderedStream = function(n) {\n arr = new Array(n);\n plt = 0;\n};\n\n/ \n * @param {number} idKey \n * @param {str
JoviHo
NORMAL
2021-09-09T04:00:34.508200+00:00
2021-09-09T04:00:34.508247+00:00
131
false
```\n/**\n * @param {number} n\n */\nvar OrderedStream = function(n) {\n arr = new Array(n);\n plt = 0;\n};\n\n/** \n * @param {number} idKey \n * @param {string} value\n * @return {string[]}\n */\nOrderedStream.prototype.insert = function(idKey, value) {\n arr[idKey-1] = value;\n let tempArr = [];\n for(let i = plt; i < arr.length;i++){\n if(typeof arr[i] != "undefined"){\n tempArr.push(arr[i]);\n plt++;\n }else{\n break;\n }\n \n }\n return tempArr;\n};\n
3
0
[]
0
design-an-ordered-stream
Java 5 Lines Solution
java-5-lines-solution-by-ferman-d0oq
\nprivate String[] values;\nprivate int i = 0;\n\npublic OrderedStream(int n) {\n\tvalues = new String[n];\n}\n\npublic List<String> insert(int idKey, String va
ferman
NORMAL
2021-05-03T12:16:32.058352+00:00
2021-05-03T12:16:32.058383+00:00
553
false
```\nprivate String[] values;\nprivate int i = 0;\n\npublic OrderedStream(int n) {\n\tvalues = new String[n];\n}\n\npublic List<String> insert(int idKey, String value) {\n\t// Below 5 lines do the magic\n\tvalues[idKey - 1] = value; \n\tList<String> res = new ArrayList<>();\n\twhile (i < values.length && values[i] != null) {\n\t\tres.add(values[i]);\n\t\ti++;\n\t}\n\treturn res;\n}\n```
3
0
['Java']
0
design-an-ordered-stream
[Python3] Solution and most importantly - Question Explained
python3-solution-and-most-importantly-qu-iohq
Problem: Question is asking to return the stream of strings starting from the location idKey. \nIdea: Keep global variable self.loc; if the idKey is equal to se
vs152
NORMAL
2021-04-08T00:10:38.622596+00:00
2021-04-08T00:10:38.622645+00:00
408
false
Problem: Question is asking to return the stream of strings starting from the location **idKey**. \nIdea: Keep global variable ```self.loc```; if the idKey is equal to ```self.loc``` then return the array of strings. Otherwise return ```[]```\n\nI hope this helps someone!\nP.S.: It\'s better to skip this question! ;p\n\n```\nclass OrderedStream:\n def __init__(self, n: int):\n self.ls = ["" for i in range(n)]\n self.loc = 0\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.ls[idKey - 1] = value\n res = []\n \n if(self.loc == (idKey - 1)):\n while(self.loc < len(self.ls) and self.ls[self.loc] != ""):\n res.append(self.ls[self.loc])\n self.loc += 1\n \n return res\n```
3
1
['Python3']
0
design-an-ordered-stream
java simple
java-simple-by-rmanish0308-0g1a
\nclass OrderedStream {\n private int ptr;\n private String[] arr;\n public OrderedStream(int n) {\n arr = new String[n+1];\n ptr = 1;\n
rmanish0308
NORMAL
2021-04-02T06:11:36.112295+00:00
2021-04-02T06:11:36.112335+00:00
224
false
```\nclass OrderedStream {\n private int ptr;\n private String[] arr;\n public OrderedStream(int n) {\n arr = new String[n+1];\n ptr = 1;\n }\n \n public List<String> insert(int idKey, String value) {\n arr[idKey] = value;\n List<String> str = new ArrayList<>();\n while(ptr < arr.length && arr[ptr] != null )\n {\n str.add(arr[ptr]);\n ptr++;\n }\n return str;\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = new OrderedStream(n);\n * List<String> param_1 = obj.insert(idKey,value);\n */\n ```\n please upvote , if u find my code easy to understand
3
1
[]
0
design-an-ordered-stream
Simple Solution in Java
simple-solution-in-java-by-s34-kl7v
\nclass OrderedStream {\n private String[] s;\n private int next;\n public OrderedStream(int n) {\n s=new String[n+1];\n next=1;\n }\n
s34
NORMAL
2021-03-01T04:02:41.368613+00:00
2021-03-01T04:02:41.368672+00:00
164
false
```\nclass OrderedStream {\n private String[] s;\n private int next;\n public OrderedStream(int n) {\n s=new String[n+1];\n next=1;\n }\n \n public List<String> insert(int id, String value) {\n s[id]=value;\n List<String> tmp=new ArrayList<>();\n if(id==next){\n while(next<s.length && s[next]!=null){\n tmp.add(s[next++]);\n }\n \n }\n return tmp;\n }\n}\n```\nPlease **upvote**, if you like the solution:)
3
0
[]
1
design-an-ordered-stream
Python solution easy and straight forward
python-solution-easy-and-straight-forwar-afax
\nclass OrderedStream:\n def __init__(self, n: int):\n self.stream = [None]*(n+1)\n self.temp = 1\n\n def insert(self, id: int, value: str)
kadarsh2k00
NORMAL
2020-11-30T09:21:08.620518+00:00
2020-11-30T09:22:05.076333+00:00
890
false
```\nclass OrderedStream:\n def __init__(self, n: int):\n self.stream = [None]*(n+1)\n self.temp = 1\n\n def insert(self, id: int, value: str) -> List[str]:\n self.stream[id] = value\n ans = []\n while self.temp < len(self.stream) and self.stream[self.temp]:\n ans.append(self.stream[self.temp])\n self.temp += 1\n return ans\n```
3
0
['Python', 'Python3']
0
design-an-ordered-stream
C# - Store id value in simple array, main part is understanding question
c-store-id-value-in-simple-array-main-pa-w7jf
csharp\npublic class OrderedStream\n{\n string[] ordered;\n int ptr;\n\n public OrderedStream(int n)\n {\n ordered = new string[n + 1];\n
christris
NORMAL
2020-11-15T04:04:58.969932+00:00
2020-11-15T04:04:58.969959+00:00
312
false
```csharp\npublic class OrderedStream\n{\n string[] ordered;\n int ptr;\n\n public OrderedStream(int n)\n {\n ordered = new string[n + 1];\n ptr = 1;\n }\n\n public IList<string> Insert(int id, string value)\n {\n ordered[id] = value;\n List<string> result = new List<string>();\n\n int i = id;\n\n if (ptr == id)\n { \n for (; i < ordered.Length; i++)\n {\n if (ordered[i] != null)\n {\n result.Add(ordered[i]);\n }\n\n else\n {\n break;\n }\n } \n \n ptr = i;\n }\n \n return result;\n }\n}\n```
3
0
[]
0
design-an-ordered-stream
🔥Beats 93.56%🔥✅Easy (C++/Java/Python) Solution With Detailed Explanation✅
beats-9356easy-cjavapython-solution-with-7864
Intuition\nThe intuition of OrderedStream class in C++ is designed to model an ordered stream of strings where the strings are inserted at specific positions, a
suyogshete04
NORMAL
2024-02-20T03:22:24.071041+00:00
2024-02-20T03:23:33.318042+00:00
586
false
# Intuition\nThe intuition of `OrderedStream` class in C++ is designed to model an ordered stream of strings where the strings are inserted at specific positions, and contiguous blocks of strings are returned from the point of insertion if they form a sequence without gaps. Let\'s analyze the approach and complexity of this implementation.\n\n\n![Screenshot 2024-02-20 084736.png](https://assets.leetcode.com/users/images/572e1e5d-36c1-46bc-80a3-9a1de5ae0b3e_1708399271.8232589.png)\n# Approach\n\n1. **Initialization (`OrderedStream` constructor):** The vector `res` is resized to `n` and filled with empty strings (or spaces, as in the provided code), indicating uninitialized positions. The pointer `ptr` is set to 0.\n\n2. **Insertion (`insert` method):**\n - The method places the input string `value` at the correct index (`idKey - 1`) in the vector `res`.\n - Starting from `ptr`, it checks for and collects all contiguous, non-empty strings in a temporary vector `s`.\n - The pointer `ptr` is updated to point to the next empty or uninitialized position in the vector `res`.\n\n\n# Complexity\n\n- **Time Complexity:**\n - The time complexity of the `insert` method is O(n) in the worst-case scenario, where `n` is the total number of elements in the stream. This worst-case scenario occurs when the last insert operation triggers the collection and return of all remaining elements in the stream.\n - However, for each individual insert operation, the time complexity can be considered amortized O(1), assuming that the distribution of insert operations and the lengths of contiguous blocks are relatively uniform over time.\n\n- **Space Complexity:**\n - The space complexity is O(n), where `n` is the size of the stream. This is due to the storage requirements of the vector `res`, which holds up to `n` strings. The temporary vector `s` returned by the `insert` method does not contribute to the overall space complexity in terms of additional data structures because its lifetime is limited to the scope of the `insert` method call.\n\n# Code\n```C++ []\nclass OrderedStream {\npublic:\n\n vector<string> res;\n int ptr = 0;\n OrderedStream(int n) {\n res.resize(n, " ");\n }\n \n vector<string> insert(int idKey, string value) {\n res[idKey-1] = value;\n vector<string> s = {};\n\n while (ptr < res.size() && res[ptr] != " ")\n s.push_back(res[ptr++]);\n\n return s;\n }\n};\n```\n```Java []\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class OrderedStream {\n private String[] stream;\n private int ptr;\n\n public OrderedStream(int n) {\n stream = new String[n];\n ptr = 0; // Pointer starts at 0, which will be incremented to point to the next index after the first insertion\n }\n\n public List<String> insert(int idKey, String value) {\n List<String> result = new ArrayList<>();\n // Adjust the idKey to 0-based index\n stream[idKey - 1] = value;\n \n // Check if the current pointer is at an element that is not null and add to result list\n while (ptr < stream.length && stream[ptr] != null) {\n result.add(stream[ptr]);\n ptr++;\n }\n\n return result;\n }\n}\n\n```\n```Python []\nfrom typing import List\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.stream = [""] * n # Initialize the stream with empty strings\n self.ptr = 0 # Pointer to track the next element to output\n\n def insert(self, idKey: int, value: str) -> List[str]:\n # Adjust for 0-based indexing\n self.stream[idKey - 1] = value\n result = []\n\n # Output all contiguous, non-empty strings starting from ptr\n while self.ptr < len(self.stream) and self.stream[self.ptr]:\n result.append(self.stream[self.ptr])\n self.ptr += 1\n\n return result\n\n```\n\n\n```
2
0
['C++']
0
design-an-ordered-stream
Solution with Runtime: 97.1% JAVA
solution-with-runtime-971-java-by-nyuton-oc1f
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
nyutonov_sh
NORMAL
2023-06-29T17:10:58.889950+00:00
2023-06-29T17:11:44.787831+00:00
375
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass OrderedStream {\n String[] arr;\n int ptr = 1;\n\n public OrderedStream(int n) {\n arr = new String[n + 1];\n }\n\n public List<String> insert(int idKey, String value) {\n ArrayList<String> k = new ArrayList<>();\n arr[idKey] = value;\n\n if (ptr != idKey) return k;\n \n for (int i = ptr; i < arr.length && arr[i] != null; i++) {\n k.add(arr[i]);\n ptr++;\n }\n\n return k;\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = new OrderedStream(n);\n * List<String> param_1 = obj.insert(idKey,value);\n */\n```
2
0
['Java']
0
design-an-ordered-stream
Easy Java Solution Beats 99% onine submissions
easy-java-solution-beats-99-onine-submis-r6rw
Code\n\nclass OrderedStream {\n private String[] data;\n private int ptr;\n\n public OrderedStream(int n) {\n data = new String[n + 1];\n
Viraj_Patil_092
NORMAL
2023-06-11T17:51:40.916845+00:00
2023-06-11T17:51:40.916897+00:00
1,164
false
# Code\n```\nclass OrderedStream {\n private String[] data;\n private int ptr;\n\n public OrderedStream(int n) {\n data = new String[n + 1];\n ptr = 1;\n }\n\n public String[] insert(int idKey, String value) {\n data[idKey] = value;\n List<String> res = new ArrayList<>();\n for (int i = ptr; i < data.length; i++) {\n if (data[i] != null) {\n res.add(data[i]);\n } else {\n ptr = i;\n break;\n }\n }\n return res.toArray(new String[0]);\n }\n}\n```
2
0
['Array', 'Hash Table', 'Design', 'Data Stream', 'Java']
0
design-an-ordered-stream
Python easy solution
python-easy-solution-by-nitw_rsa-mmqq
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.arr = [-1] * (n + 1)\n self.n = n\n self.ptr = 1\n\n\n def insert(self,
nitw_rsa
NORMAL
2022-10-10T14:52:12.735269+00:00
2022-10-10T14:52:12.735298+00:00
1,627
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.arr = [-1] * (n + 1)\n self.n = n\n self.ptr = 1\n\n\n def insert(self, idKey: int, value: str) -> List[str]:\n ans = []\n self.arr[idKey] = value\n \n while self.ptr <= self.n and self.arr[self.ptr] != -1:\n ans.append(self.arr[self.ptr])\n self.ptr += 1\n \n return ans\n \n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```
2
0
[]
0
design-an-ordered-stream
C++ solution with explanation
c-solution-with-explanation-by-maitreya4-hi08
OG solution: https://leetcode.com/problems/design-an-ordered-stream/discuss/936090/C%2B%2B-Array\nFirst create a vector that will store the strings in the strea
maitreya47
NORMAL
2022-09-26T19:08:04.425685+00:00
2022-09-26T19:08:34.555023+00:00
1,623
false
OG solution: https://leetcode.com/problems/design-an-ordered-stream/discuss/936090/C%2B%2B-Array\nFirst create a vector that will store the strings in the stream and over which we will iterate. Resize it to n+1 because the idKey will start from 1 and not 0.\nIn the insert function, assign the value to appropriate idKey and check if the ptr is less than the stream size (because in the next step we are indexing into s to check if the value is empty at that index. Once checked that we have value, add that to return vector and go ahead, until we have some value at those indexes. \n```\nclass OrderedStream {\npublic:\n int ptr = 1;\n vector<string> s;\n OrderedStream(int n) {\n s.resize(n+1);\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> ret;\n s[idKey] = value;\n while(ptr<s.size() && !s[ptr].empty())\n ret.push_back(s[ptr++]);\n return ret;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n ```\n Time complexity: O(N)\n Space complexity: O(N)
2
0
['C', 'C++']
0
design-an-ordered-stream
✅C++||String Array || Easy Solution
cstring-array-easy-solution-by-indresh14-5y6g
\nclass OrderedStream {\npublic:\n string dp[1005];\n int ptr;\n OrderedStream(int n){\n ptr = 1;\n }\n \n vector<string> insert(int id
indresh149
NORMAL
2022-08-25T16:19:25.470687+00:00
2022-08-25T16:19:25.470724+00:00
631
false
```\nclass OrderedStream {\npublic:\n string dp[1005];\n int ptr;\n OrderedStream(int n){\n ptr = 1;\n }\n \n vector<string> insert(int idKey, string value) {\n dp[idKey] = value;\n if(dp[ptr] != ""){\n vector<string> ans;\n for(int i=ptr;i<=1005;i++){\n if(dp[i] != ""){\n ans.push_back(dp[i]);\n ptr = i+1;\n }\n else\n {\n break;\n }\n }\n return ans;\n }\n return {};\n }\n};\n\n```\n```\nPlease upvote if it was helpful for you, thank you!\n```
2
0
['C']
0
design-an-ordered-stream
JAVA well-formatted solution
java-well-formatted-solution-by-maxbaldi-rnln
The basic idea is to have a pointer and an array of strings and move the pointer with each insert if required.\nSpace: O(n), Time: O(n)\n\n\nimport java.util.Ar
maxbaldin
NORMAL
2022-07-21T07:55:25.947556+00:00
2022-07-21T07:55:25.947611+00:00
580
false
The basic idea is to have a pointer and an array of strings and move the pointer with each insert if required.\nSpace: O(n), Time: O(n)\n\n```\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass OrderedStream {\n String[] stream;\n int ptr = 0;\n\n public OrderedStream(int n) {\n this.stream = new String[n];\n }\n\n public List<String> insert(int idKey, String value) {\n stream[idKey - 1] = value;\n\n List<String> result = new ArrayList<>();\n for (int i = ptr; i < stream.length; i++) {\n String element = this.stream[i];\n\n if (element == null) {\n break;\n }\n result.add(element);\n ptr++;\n }\n\n return result;\n }\n}\n```
2
0
['Array', 'Java']
0
design-an-ordered-stream
python 3 || simple solution
python-3-simple-solution-by-derek-y-vqba
```\nclass OrderedStream:\n\n def init(self, n: int):\n self.stream = [\'\'] * (n + 1)\n self.i = 0\n\n def insert(self, idKey: int, value:
derek-y
NORMAL
2022-06-13T06:08:28.567167+00:00
2022-06-13T06:13:06.624721+00:00
719
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.stream = [\'\'] * (n + 1)\n self.i = 0\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.stream[idKey - 1] = value\n res = []\n\n while self.stream[self.i]:\n res.append(self.stream[self.i])\n self.i += 1\n \n return res
2
0
['Array', 'Python', 'Python3']
1
design-an-ordered-stream
Python Commented beats 90% | Iteration, Codesplitting
python-commented-beats-90-iteration-code-3v8e
class OrderedStream:\n\n def init(self, n: int):\n self.arr = [None]*n # initialize the array for size n\n self.ptr = 0 # remeber where the stream
masterofnone
NORMAL
2022-04-30T14:22:42.057155+00:00
2022-04-30T14:22:42.057193+00:00
575
false
class OrderedStream:\n\n def __init__(self, n: int):\n self.arr = [None]*n # initialize the array for size n\n self.ptr = 0 # remeber where the stream pointer is at\n def get_chunk(self):\n chunk = []\n \n # Construct the next chunk by checking bounds, and ensuring value is not None\n while self.ptr < len(self.arr) and self.arr[self.ptr]:\n chunk.append(self.arr[self.ptr])\n self.ptr+=1 # Update the pointer\n return chunk\n def insert(self, idKey: int, value: str) -> List[str]:\n # set the arr\'s 0-indexed value to the incoming value\n self.arr[idKey-1] = value\n return self.get_chunk() # then return the chunk
2
0
['Python', 'Python3']
0
design-an-ordered-stream
Easy Typescript solution
easy-typescript-solution-by-buzzlightyea-e3x5
\nclass OrderedStream {\n stream: string[]\n index: number;\n constructor(n: number) {\n this.stream = []\n this.index = 0;\n }\n\n
BuzzLightyearAldrin
NORMAL
2022-04-23T18:29:31.265969+00:00
2022-04-23T18:29:31.266015+00:00
157
false
```\nclass OrderedStream {\n stream: string[]\n index: number;\n constructor(n: number) {\n this.stream = []\n this.index = 0;\n }\n\n insert(idKey: number, value: string): string[] {\n this.stream[idKey-1] = value;\n \n if(this.index === idKey-1) {\n while(this.stream[this.index]) {\n this.index++;\n }\n }\n return this.stream.slice(idKey-1,this.index);\n }\n}\n```
2
0
['TypeScript']
0
design-an-ordered-stream
JAVA | Okay, sweetie, I know you think you’re explaining yourself, but you’re really not.
java-okay-sweetie-i-know-you-think-youre-3n2u
While I am reading the description, the voice of Penny from BIG BANG THEORY pop out.\n\n*Penny: Okay, sweetie, I know you think you\u2019re explaining yourself,
CrackItSean
NORMAL
2022-04-15T04:28:40.419141+00:00
2022-04-15T04:28:40.419188+00:00
334
false
While I am reading the description, the voice of Penny from BIG BANG THEORY pop out.\n\n****Penny: Okay, sweetie, I know you think you\u2019re explaining yourself, but you\u2019re really not.****\n\n```\nclass OrderedStream {\n String values[];\n int top=0;\n public OrderedStream(int n) {\n this.values=new String[n+1];\n }\n \n public List<String> insert(int idKey, String value) {\n LinkedList<String> res=new LinkedList();\n values[idKey]=value; \n for(int i=top+1;i<=idKey;i++){\n if(values[i]==null){\n break;\n }\n top=i;\n }\n if(top<idKey){\n return res;\n }\n for(int i=idKey;i<values.length;i++){\n if(values[i]!=null){\n top=i;\n res.add(values[i]);\n }else{\n return res;\n }\n }\n return res;\n }\n}\n```
2
0
['Java']
0
design-an-ordered-stream
2 Easy to understand and simple Python3 Solutions using List and Dictionary
2-easy-to-understand-and-simple-python3-86i21
Method 1 - using List\n\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.val = [0]*n\n self.cur = 0\n\n de
harshitpoddar09
NORMAL
2022-02-13T19:36:12.289943+00:00
2022-02-13T19:36:12.289983+00:00
117
false
**Method 1 - using List**\n\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.val = [0]*n\n self.cur = 0\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.val[idKey-1] = value\n ans = []\n for i in range(self.cur,self.n):\n if self.val[i] == 0:\n self.cur = i\n return ans\n ans.append(self.val[i])\n return ans\n \n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```\n\nRuntime - 477ms\nMemory Usage - 14.7mb\n\n**Method 2 - using Dictionary**\n\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.d = {}\n self.cur = 1\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.d[idKey] = value\n ans = []\n for i in range(self.cur,self.n+1):\n if i not in self.d:\n self.cur = i\n return ans\n ans.append(self.d[i])\n return ans\n \n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```\n\nRuntime - 228ms\nMemory Usage - 14.7mb\n\nFor solutions to other LeetCode problems that I have solved, you can check my github repository :\nhttps://github.com/harshitpoddar09/LeetCode-Solutions\n\nPlease upvote if you found this useful so that others can get help as well!\n
2
0
[]
0
design-an-ordered-stream
[JS] O(n) solution using hashmap
js-on-solution-using-hashmap-by-wintryle-3keb
\nclass OrderedStream {\n constructor(n) {\n this.map = new Map();\n this.key = 1;\n }\n insert(idKey, value) {\n this.map.set(idK
wintryleo
NORMAL
2022-01-17T21:35:40.309373+00:00
2022-01-17T21:35:40.309418+00:00
423
false
```\nclass OrderedStream {\n constructor(n) {\n this.map = new Map();\n this.key = 1;\n }\n insert(idKey, value) {\n this.map.set(idKey, value);\t\t// add the key-value to the map\n const result = [];\n\t\t// until able to find the key in the map, add it to the resultant chunk\n\t\t// each time the next greater key is found, it will start the result from that key value\n while(this.map.has(this.key)) { // O(n)\n result.push(this.map.get(this.key));\n ++this.key;\n }\n return result;\n }\n}\n```\nTime Complexity = O(n)\nSpace Complexity = O(n)
2
0
['JavaScript']
0
design-an-ordered-stream
Java Solution
java-solution-by-himanshubhoir-prl7
\nclass OrderedStream {\n String[] arr;\n int ptr;\n public OrderedStream(int n) {\n arr = new String[n];\n ptr = 0;\n }\n \n pu
HimanshuBhoir
NORMAL
2022-01-11T05:01:33.865099+00:00
2022-09-15T13:37:01.450206+00:00
336
false
```\nclass OrderedStream {\n String[] arr;\n int ptr;\n public OrderedStream(int n) {\n arr = new String[n];\n ptr = 0;\n }\n \n public List<String> insert(int idKey, String value) {\n arr[idKey-1] = value;\n List<String> list = new ArrayList<>();\n while(ptr < arr.length && arr[ptr] != null){\n list.add(arr[ptr++]);\n }\n return list;\n }\n}\n```
2
0
['Java']
0
design-an-ordered-stream
SIMPLEST SOLUTION - C++
simplest-solution-c-by-aadi_01-cdwd
\nclass OrderedStream {\npublic:\n vector<string> temporary;\n int currentPosition = 0;\n \n OrderedStream(int n) {\n temporary.resize(n);\n
aadi_01
NORMAL
2021-09-14T07:13:22.347345+00:00
2021-09-14T07:13:22.347377+00:00
195
false
```\nclass OrderedStream {\npublic:\n vector<string> temporary;\n int currentPosition = 0;\n \n OrderedStream(int n) {\n temporary.resize(n);\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> answer;\n temporary[ idKey - 1 ] = value;\n \n while( currentPosition < temporary.size() && temporary[currentPosition] != "") {\n answer.push_back(temporary[currentPosition]);\n currentPosition++;\n }\n \n return answer;\n }\n};\n```
2
0
[]
0
design-an-ordered-stream
Java solution
java-solution-by-rkxyiiyk2-mg4o
\nclass OrderedStream {\n \n private int lo = 1;\n private String[] vals;\n\n public OrderedStream(int n) {\n vals = new String[n+1];\n }\
rkxyiiyk2
NORMAL
2021-08-06T20:15:53.890263+00:00
2021-08-06T20:15:53.890295+00:00
449
false
```\nclass OrderedStream {\n \n private int lo = 1;\n private String[] vals;\n\n public OrderedStream(int n) {\n vals = new String[n+1];\n }\n \n public List<String> insert(int idKey, String value) {\n vals[idKey] = value;\n \n var retList = new ArrayList<String>();\n while (lo < vals.length && vals[lo] != null) {\n retList.add(vals[lo++]);\n }\n return retList;\n }\n}\n```
2
0
['Java']
0
design-an-ordered-stream
Python 93% fast solution - 5 lines only
python-93-fast-solution-5-lines-only-by-yopym
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.li=[0]*(n+1)\n self.ptr=0\n\n def insert(self, idKey: int, value: str) -> List
srhr17
NORMAL
2021-06-26T04:56:56.658277+00:00
2021-06-26T04:57:48.069807+00:00
575
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.li=[0]*(n+1)\n self.ptr=0\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.li[idKey-1]=value\n end=self.li.index(0)\n k=self.ptr\n self.ptr=end\n return self.li[k:end]\n \n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```
2
0
['Python', 'Python3']
0
design-an-ordered-stream
C++ short and easy
c-short-and-easy-by-rap_tors-hoqo
\nclass OrderedStream {\npublic:\n vector<string>v;\n int i; //global index variable, to help us return output as vector gets filled\n OrderedStream(in
rap_tors
NORMAL
2021-06-15T16:34:25.174528+00:00
2021-06-15T16:34:25.174591+00:00
341
false
```\nclass OrderedStream {\npublic:\n vector<string>v;\n int i; //global index variable, to help us return output as vector gets filled\n OrderedStream(int n) {\n v.resize(n, ""), i = 0;\n }\n vector<string> insert(int id, string value) {\n v[id - 1] = value; //since id is starting from 1\n vector<string>res;\n while(i < v.size() and v[i].size() != 0)\n res.push_back(v[i++]); //keep pushing the continuous values\n return res;\n }\n};\n```
2
0
['C++']
0
design-an-ordered-stream
Javascript : Simple And Concise Solution
javascript-simple-and-concise-solution-b-rg18
\nvar OrderedStream = function(n) {\n this.pointer = 0;\n this.streamArray = new Array(n).fill(undefined);\n};\nOrderedStream.prototype.insert = function(
jadhamwi21
NORMAL
2021-05-22T15:11:59.287725+00:00
2021-05-22T15:12:21.468251+00:00
340
false
```\nvar OrderedStream = function(n) {\n this.pointer = 0;\n this.streamArray = new Array(n).fill(undefined);\n};\nOrderedStream.prototype.insert = function(idKey, value) {\n this.streamArray[idKey-1] = value;\n if(this.streamArray[this.pointer] === undefined){\n return [];\n }else{\n const result = [];\n while(this.streamArray[this.pointer] !== undefined){\n result.push(this.streamArray[this.pointer]);\n this.pointer++;\n }\n return result;\n }\n};\n```
2
0
['JavaScript']
1
design-an-ordered-stream
Python :: Priority Queue
python-priority-queue-by-tuhinnn_py-4w4g
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.ans = []\n \n self.counter = 1\n\n def insert(self,
tuhinnn_py
NORMAL
2021-05-19T18:13:57.910383+00:00
2021-05-19T18:13:57.910432+00:00
111
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.ans = []\n \n self.counter = 1\n\n def insert(self, idKey: int, value: str) -> List[str]:\n ans = []\n heapq.heappush(self.ans, (idKey, value))\n \n x = heapq.heappop(self.ans) if self.ans else None\n while x and self.counter == x[0]:\n ans.append(x[1])\n x = heapq.heappop(self.ans) if self.ans else None\n self.counter += 1\n if x:\n heapq.heappush(self.ans, x)\n \n return ans\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```
2
0
[]
0
design-an-ordered-stream
Self explanatory O(n) easy to understand using Java
self-explanatory-on-easy-to-understand-u-hajt
\nclass OrderedStream {\n private String[] streams;\n private int counter = 0;\n public OrderedStream(int n) {\n streams = new String[n];\n }
i_m_groot
NORMAL
2021-05-16T17:15:43.731143+00:00
2021-05-16T17:15:43.731186+00:00
266
false
```\nclass OrderedStream {\n private String[] streams;\n private int counter = 0;\n public OrderedStream(int n) {\n streams = new String[n];\n }\n \n public List<String> insert(int idKey, String value) {\n List<String> stream = new ArrayList<String>();\n streams[idKey-1] = value;\n while (counter < streams.length && streams[counter] != null) \n {\n\t\tstream.add(streams[counter]);\n\t\tcounter++;\n\t }\n return stream;\n }\n}\n```\n\nPS: Do upvote if you like the solution and understood it easily :-)
2
0
['Array', 'Java']
0
design-an-ordered-stream
C++ faster than 95%, with explanation
c-faster-than-95-with-explanation-by-saj-8mir
Basic Idea\nAs we are given the total number of strings to be inserted, we can use an array of strings of length n to save them, according to their index (which
sajals5031
NORMAL
2021-05-16T09:08:50.620752+00:00
2021-05-16T09:08:50.620795+00:00
323
false
## Basic Idea\nAs we are given the total number of strings to be inserted, we can use an array of strings of length n to save them, according to their index (which is idKey -1). We also keep track of the next index to start the chunk building from, i.e the next unpublished string in the stream.\n\nThe stream is initialized to n empty strings, (as sentinels), and next is set to 0\nFor each insert call, we first put the given string at its position. Then we traverse the array from the position next, uptil we find another empty string and keep on adding them to the chunk, this way we find the largest possible contiguous chunk starting from the position next. Then we update next to the new empty position, and continue.\n\n## Code\n```\nclass OrderedStream {\n vector<string> stream;\n int next;\n int l;\npublic:\n OrderedStream(int n) {\n stream = vector<string>(n, "");\n next = 0;\n l = n;\n }\n vector<string> insert(int idKey, string value) {\n vector<string> chunk;\n stream[idKey-1] = value;\n if(idKey == next + 1) {\n //we found the next string in the stream\n while(next < l && stream[next] != "") {\n chunk.push_back(stream[next]);\n next++;\n }\n }\n return chunk;\n }\n};\n```\n\n## Complexity\n**Time:** **O(n)**, as in all cases, the sum of the lengths of all the chunks will be exactly n, and the runtime of the function depends on the size of the current chunk.\n**Space: O(n)**, as an array of n strings is required to store the stream itself, and each chunk is stored separately, whose lengths add upto n.\n
2
0
['C']
2
design-an-ordered-stream
C++ Clean code
c-clean-code-by-hammerhead09-tfkd
```\nclass OrderedStream {\n vector stream;\n int pointer;\npublic:\n OrderedStream(int n) {\n stream.resize(n + 1);\n pointer = 1;\n
hammerhead09
NORMAL
2021-01-27T22:52:08.367161+00:00
2021-01-27T22:52:08.367201+00:00
213
false
```\nclass OrderedStream {\n vector<string> stream;\n int pointer;\npublic:\n OrderedStream(int n) {\n stream.resize(n + 1);\n pointer = 1;\n }\n \n vector<string> insert(int id, string value) {\n stream[id] = value;\n vector<string> result;\n \n while (pointer < stream.size() and not stream[pointer].empty())\n result.push_back(stream[pointer++]);\n \n return result;\n }\n};
2
0
[]
0
design-an-ordered-stream
Python3 Solution
python3-solution-by-mxmb-2sr2
Hard mode: take a shot every time you have to type self.\npython\nclass OrderedStream:\n \n def __init__(self, n: int):\n self.S = [None]*(n+1)\n
mxmb
NORMAL
2021-01-12T23:56:32.079944+00:00
2021-01-12T23:57:36.697991+00:00
492
false
Hard mode: take a shot every time you have to type `self`.\n```python\nclass OrderedStream:\n \n def __init__(self, n: int):\n self.S = [None]*(n+1)\n self.i = 1\n \n def insert(self, id: int, value: str) -> List[str]:\n self.S[id] = value\n while self.i < len(self.S):\n if not self.S[self.i]:\n break\n self.i += 1\n return self.S[id:self.i]\n```
2
0
['Python', 'Python3']
0
design-an-ordered-stream
Java solution
java-solution-by-thilms-6qzf
private String[] arr;\n public OrderedStream(int n) {\n arr = new String[n];\n }\n\n public List<String> insert(int id, String value) {\n
thilms
NORMAL
2020-12-26T19:44:35.222039+00:00
2020-12-26T19:47:04.297093+00:00
159
false
``` private String[] arr;\n public OrderedStream(int n) {\n arr = new String[n];\n }\n\n public List<String> insert(int id, String value) {\n int index = (id-1);\n\t\tList<String> list = new ArrayList<>();\n \n\t\tif(index < 0 || index > arr.length) return list;\n \n arr[index] = value;\n for(int i=0; i< arr.length; i++){\n if(arr[i] == null) return list;\n else {\n if(i >= index){\n list.add(arr[i]);\n }\n }\n }\n return list;\n }\n```
2
0
[]
1
design-an-ordered-stream
C++ faster than 99.33%, memory usage less than 97.76%
c-faster-than-9933-memory-usage-less-tha-kthn
\nclass OrderedStream {\n public:\n // Initialize "pointer" (btw not an actual pointer) & stream vector\n int ptr=0;\n vector<string> st
hamptonjc
NORMAL
2020-11-21T02:08:09.031549+00:00
2020-11-21T02:08:09.031583+00:00
309
false
```\nclass OrderedStream {\n public:\n // Initialize "pointer" (btw not an actual pointer) & stream vector\n int ptr=0;\n vector<string> stream;\n\n OrderedStream(int n) {\n // Constructor makes stream vector of size n\n vector<string> strm(n);\n stream = strm;\n }\n\n vector<string> insert(int id, string value) {\n // decrease id by one to correspond with vector idx (i.e. start from 0)\n --id;\n \n\t\t\t// create a vector to be returned by function\n vector<string> returnVec;\n \n\t\t\t// at index id in stream vector, set the value\n stream[id] = value;\n \n // if stream at index ptr is not empty...\n if (stream[ptr] != "") {\n \n\t\t\t\t// create a new int to mark the start index\n int start_idx = ptr;\n \n\t\t\t\t// while stream at index ptr is not empty, move ptr to the next index\n while (stream[ptr] != "" && ptr != stream.size()) ptr++;\n \n\t\t\t\t// set returnVec to a sliced version of the stream vector\n // (i.e. stream[start_idx:ptr])\n returnVec = vector<string>(stream.begin()+start_idx, stream.begin()+ptr);\n }\n return returnVec;\n }\n}; \n\n```\n\nJust my first go at it, im sure it could be improved. Welcome to any suggestions.
2
0
['C']
0
design-an-ordered-stream
C++ 98%
c-98-by-eridanoy-dmwy
\nclass OrderedStream {\npublic:\n OrderedStream(int n) {\n vec.resize(n);\n }\n \n vector<string> insert(int id, const string& value) {\n
eridanoy
NORMAL
2020-11-17T22:56:52.798858+00:00
2020-11-17T22:58:44.217369+00:00
361
false
```\nclass OrderedStream {\npublic:\n OrderedStream(int n) {\n vec.resize(n);\n }\n \n vector<string> insert(int id, const string& value) {\n vec[--id]=value;\n vector<string> ans;\n while(pos<vec.size()) {\n if(vec[pos].empty()) break;\n ans.push_back(vec[pos++]);\n }\n return ans;\n }\nprivate:\n vector<string> vec;\n int pos=0;\n};\n```
2
0
['C']
0
design-an-ordered-stream
100% fastest in C
100-fastest-in-c-by-fabiengaubert-mm0o
\ntypedef struct {\n int ptr;\n int size;\n char** value;\n} OrderedStream;\n\n\nOrderedStream* orderedStreamCreate(int n) {\n OrderedStream *ordere
fabiengaubert
NORMAL
2020-11-17T12:37:12.400439+00:00
2020-11-17T12:37:12.400475+00:00
292
false
```\ntypedef struct {\n int ptr;\n int size;\n char** value;\n} OrderedStream;\n\n\nOrderedStream* orderedStreamCreate(int n) {\n OrderedStream *orderedStream = (OrderedStream*)malloc(sizeof(OrderedStream));\n orderedStream->ptr=1;\n orderedStream->size=n;\n orderedStream->value=(char**)malloc(sizeof(char*)*n);\n for(int i=0;i<n; i++){\n *(orderedStream->value+i)=NULL;\n }\n return orderedStream;\n}\n\nchar ** orderedStreamInsert(OrderedStream* obj, int id, char * value, int* retSize) {\n *(obj->value+id-1)=value;\n int count=0;\n if(id==obj->ptr){\n while(id-1+count<obj->size&&*(obj->value+id-1+count)!=NULL)\n count++;\n obj->ptr=id+count;\n }\n *retSize=count;\n return obj->value+id-1;\n}\n\nvoid orderedStreamFree(OrderedStream* obj) {\n free(obj->value);\n free(obj);\n}\n```
2
0
[]
0
design-an-ordered-stream
65ms Java faster than 100% w/Comments
65ms-java-faster-than-100-wcomments-by-r-c6c6
If any question feel free to ask.\n\nclass OrderedStream {\n String[] arr; //Stores the String values in the id\'th index.\n int size; //size of orderedst
reuzun
NORMAL
2020-11-15T13:44:01.083368+00:00
2020-11-15T13:45:10.992296+00:00
345
false
If any question feel free to ask.\n```\nclass OrderedStream {\n String[] arr; //Stores the String values in the id\'th index.\n int size; //size of orderedstream\n int ptr; //pointer to hold current position\n \n public OrderedStream(int n) {\n size = n;\n arr = new String[size+1]; //size+1 cuz of id starts from 1.\n ptr = 1; //it starts from 1 cuz of id is between 1 and n\n }\n \n public List<String> insert(int id, String value) {\n List<String> list = new ArrayList<>(); //local list to return.\n arr[id] = value; //puts the string value to the index of id.\n\t\t\n\t\t//This loop iterates untill our pointer points null. While it is not null\n\t\t//It points value of pointer and add it to our List to return.\n while(ptr<=size){\n if(arr[ptr]==null)break;//If our pointer is null then we can break the loop.\n list.add(arr[ptr++]);//If pointer is not null then add pointer value to list and increase the pointer for next values.\n }\n\t\t\n return list;\n } \n}\n```
2
0
['Java']
1
design-an-ordered-stream
Java Simple HashMap
java-simple-hashmap-by-vikrant_pc-sqra
\nclass OrderedStream {\n int index = 1;\n Map<Integer, String> map = new HashMap<>();\n \n public OrderedStream(int n) {\n }\n \n public L
vikrant_pc
NORMAL
2020-11-15T08:15:36.578943+00:00
2020-11-15T08:20:31.545196+00:00
243
false
```\nclass OrderedStream {\n int index = 1;\n Map<Integer, String> map = new HashMap<>();\n \n public OrderedStream(int n) {\n }\n \n public List<String> insert(int id, String value) {\n map.put(id, value);\n List<String> result = new ArrayList<>();\n while (map.containsKey(index))\n result.add(map.get(index++));\n return result;\n }\n}\n```
2
0
[]
0
design-an-ordered-stream
Javascript solution using array - 184 ms
javascript-solution-using-array-184-ms-b-huo5
\nvar OrderedStream = function(n) {\n this.arr = new Array(n);\n this.index = 0;\n};\n\nOrderedStream.prototype.insert = function(id, value) {\n this.a
eforce
NORMAL
2020-11-15T06:46:10.921801+00:00
2020-11-15T06:46:10.921833+00:00
214
false
```\nvar OrderedStream = function(n) {\n this.arr = new Array(n);\n this.index = 0;\n};\n\nOrderedStream.prototype.insert = function(id, value) {\n this.arr[id - 1] = value;\n const result = [];\n while (this.arr[this.index]) {\n result.push(this.arr[this.index++]);\n }\n return result;\n};\n```
2
0
[]
0
design-an-ordered-stream
Golang solution with explanation of problem
golang-solution-with-explanation-of-prob-duiq
Intuition\nMy intuition here was to look at tons of solutions on youtube & leetcode until I could understand the problem because the description made no mention
gcrobertson
NORMAL
2023-12-17T04:03:34.390690+00:00
2023-12-17T04:03:34.390717+00:00
17
false
# Intuition\nMy intuition here was to look at tons of solutions on youtube & leetcode until I could understand the problem because the description made no mention of a pointer.\n\n# Approach\nWhat the description to the problem completely leaves out, is that our class needs two things:\n- A container for the values\n - This list is stored as a slice in Go, which is 0-indexed\n - The id arguments will be given 1-indexed\n- A pointer to an index in our list, which is initialized at the beginning and stays there until the first element in our list gets inserted.\n\nUntil the first element [id 1 in argument, index 0 in this.list] in our list is inserted, we will always return an empty slice.\n\nWhen the first element is inserted, we move the pointer until it arrives at an index in this.slice which has no value.\n\nTake the given example where we initalize an empty slice of size 5:\n\nvalue: ["","","","",""]\nindex: [ 0, 1, 2, 3, 4]\nptr: index 0\n\n1. Insert [3, "ccccc"]\n\nvalue: ["","","ccccc","",""]\nindex: [ 0, 1, 2, 3, 4]\nptr begin: index 0\nptr end: index 0\n\nThe pointer remains set to index 0 in this.list so we return [""].\n\n2. Insert [1, "aaaaa"]\n\nvalue: ["aaaaa","","ccccc","",""]\nindex: [ 0, 1, 2, 3, 4]\nptr begin: index 0\nptr end: index 1\n\nThe pointer was pointing to index 0, but it\'s no longer empty, so we move it until it points to an empty index.\nptr begin: index 0\nptr end: index 1\n\nWe return this.list["aaaaa"] because that is the longest list of non-empty values beginning from where the ptr started.\n\n3. Insert [2, "bbbbb"]\nvalue: ["aaaaa","bbbbb","ccccc","",""]\nindex: [ 0, 1, 2, 3, 4]\nptr begin: index 1\nptr end: index 3\n\nThe ptr started at index 1, which is no longer empty, so we move it until it points to an empty index. That moves the ptr to index 3.\n\nWe return this.list["bbbbb","ccccc"] because that is the longest list of non-empty values beginning from where the ptr started.\n\n4. Insert [5, "eeeee"]\nvalue: ["aaaaa","bbbbb","ccccc","","eeeee"]\nindex: [ 0, 1, 2, 3, 4]\nptr begin: index 3\nptr end: index 3\n\nWe inserted into the last index, but the index we are pointing at is still empty so we return this.list[""]\n\n5. Insert [4, "ddddd"]\nvalue: ["aaaaa","bbbbb","ccccc","ddddd","eeeee"]\nindex: [ 0, 1, 2, 3, 4]\nptr begin: index 3\nptr end: index 5 (Up to but not including this element, the end of list)\n\nWe inserted where the pointer was pointing, so we move the pointer until it points to an empty index or the end of the list. We arrive at the end of the list.\n\nWe return ["ddddd","eeeee"]\n\nI hope this explanation was helpful, please upvote if you liked it.\n\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\ntype OrderedStream struct {\n ptr int\n list []string\n}\n\nfunc Constructor(n int) OrderedStream {\n return OrderedStream{\n list : make([]string, n),\n // The pointer starts at the first index [0] in this.list.\n ptr : 0,\n }\n}\n\nfunc (this *OrderedStream) Insert(id int, value string) []string {\n // Always insert. It is a 1-indexed id in a 0-indexed slice, so subtract 1\n this.list[id-1] = value\n\n // Only when a value at the first index [0] is added do we move this.ptr.\n // Until then, we return empty strings.\n if this.list[this.ptr] == "" {\n return []string{}\n }\n\n // At this point, we have a value at this.ptr.\n // We increment this.ptr until we find an empty value or we reach\n // the end of this.list.\n var end = this.ptr\n for this.ptr < len(this.list) && this.list[this.ptr] != "" {\n this.ptr++\n end++\n }\n // Return the sub-array of values.\n return this.list[id-1:end]\n}\n```
1
0
['Go']
0
design-an-ordered-stream
86ms Beats 94.33% of users with C++
86ms-beats-9433-of-users-with-c-by-surya-j1bg
Intuition\nThe problem involves designing a data structure to handle an ordered stream of strings. My initial thoughts are to use a vector to store the strings
surya_26
NORMAL
2023-11-22T21:39:19.122222+00:00
2023-11-22T21:39:19.122261+00:00
208
false
# Intuition\nThe problem involves designing a data structure to handle an ordered stream of strings. My initial thoughts are to use a vector to store the strings and a pointer to keep track of the current position in the stream.\n\n# Approach\n1. **Initialization:**\n - Create a vector `data` to store the strings. The size of the vector is set to `n + 1` to allow for 1-based indexing.\n - Initialize a pointer `ptr` to 1, indicating the starting position in the stream.\n\n2. **Insert Operation:**\n - In the `insert` method, update the `data` vector at the specified index (`id`) with the given value.\n - After updating, check the subsequent positions in the vector. If they contain non-empty strings, add them to the result vector and update the pointer.\n\n3. **Return Result:**\n - Return the result vector containing the strings encountered in the stream.\n\n# Complexity\n- Time complexity:\n - The `insert` operation has a time complexity of $$O(1)$$ for updating the vector at a specific index.\n - The overall time complexity depends on the number of non-empty strings encountered in the stream.\n\n- Space complexity:\n - The space complexity is $$O(n)$$, where $$n$$ is the size of the vector used to store the strings in the stream.\n# Code\n```\nclass OrderedStream {\nprivate:\n vector<string> data;\n int ptr;\n\npublic:\n OrderedStream(int n) {\n data.resize(n + 1);\n ptr = 1;\n }\n vector<string> insert(int id, string value)\n {\n data[id]=value;\n vector<string> ans; \n while(ptr<data.size()&&!data[ptr].empty())\n {\n ans.push_back(data[ptr]);\n ptr++;\n }\n\n return ans;\n }\n};\n\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```
1
0
['Array', 'Hash Table', 'Design', 'Data Stream', 'C++']
0
design-an-ordered-stream
Decent results.
decent-results-by-domarx-9e9e
Intuition\n Describe your first thoughts on how to solve this problem. \nWhen idKey - 1 != ptr we simply return empty array of strings.\nWhen idKey - 1 == ptr w
domarx
NORMAL
2023-07-16T10:24:24.933907+00:00
2023-07-16T10:24:24.933928+00:00
13
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhen `idKey - 1 != ptr` we simply return empty array of strings.\nWhen `idKey - 1 == ptr` we iterate through array until we find an empty slot. Collect all strings in range of `ptr - idKey - 1`.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) ???\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nBecause we need to keep OrderedStream so we could collect arrays of strings ??\n# Code\n```\ntypedef struct {\n char **list;\n int ptr;\n int size;\n} OrderedStream;\n\n\nOrderedStream* orderedStreamCreate(int n) {\n OrderedStream *stream = malloc(sizeof(OrderedStream));\n stream->list = calloc(n, sizeof(char *));\n for (int i = 0; i < n; i++) { stream->list[i] = ""; }\n stream->ptr = 0;\n stream->size = n;\n\n return stream; \n}\n\nchar ** orderedStreamInsert(OrderedStream* obj, int idKey, char * value, int* retSize) {\n int size = 0;\n int idx = idKey - 1;\n\n obj->list[idx] = value;\n\n for (; (obj->ptr < obj->size) && strcmp(obj->list[obj->ptr], "") != 0; obj->ptr++) {\n size++;\n }\n\n if (size == 0){\n char str[] = {};\n return str;\n }\n\n char **result = malloc(size * sizeof(char *));\n for (int i = 0; idx < obj->ptr; i++) { result[i] = obj->list[idx++]; }\n\n *retSize = size;\n return result;\n}\n\nvoid orderedStreamFree(OrderedStream* obj) {\n if (obj == 0) { return; }\n\n free(obj->list);\n free(obj);\n}\n\n/**\n * Your OrderedStream struct will be instantiated and called as such:\n * OrderedStream* obj = orderedStreamCreate(n);\n * char ** param_1 = orderedStreamInsert(obj, idKey, value, retSize);\n \n * orderedStreamFree(obj);\n*/\n```
1
0
['C']
0
design-an-ordered-stream
C# using array
c-using-array-by-dmitriy-maksimov-j6sg
Code\n\npublic class OrderedStream\n{\n private readonly string?[] _data;\n private int _ptr;\n\n public OrderedStream(int n)\n {\n _data = n
dmitriy-maksimov
NORMAL
2023-06-14T10:45:27.856024+00:00
2023-06-14T10:45:27.856058+00:00
181
false
# Code\n```\npublic class OrderedStream\n{\n private readonly string?[] _data;\n private int _ptr;\n\n public OrderedStream(int n)\n {\n _data = new string[n + 1];\n _ptr = 1;\n }\n\n public IList<string> Insert(int idKey, string value)\n {\n _data[idKey] = value;\n var res = new List<string>();\n for (var i = _ptr; i < _data.Length; i++)\n {\n if (_data[i] != null)\n {\n res.Add(_data[i]!);\n }\n else\n {\n _ptr = i;\n break;\n }\n }\n\n return res;\n }\n}\n\n```
1
0
['C#']
0
design-an-ordered-stream
Dart Solution
dart-solution-by-ahmedwafik-slxq
\nclass OrderedStream {\n late final List<String> data;\n int pointer = 0;\n\n OrderedStream(int n) {\n data = List<String>.filled(n, \'\');\n }\n
AhmedWafik
NORMAL
2023-05-11T11:14:53.163676+00:00
2023-05-11T11:14:53.163706+00:00
17
false
```\nclass OrderedStream {\n late final List<String> data;\n int pointer = 0;\n\n OrderedStream(int n) {\n data = List<String>.filled(n, \'\');\n }\n \n List<String> insert(int idKey, String value) {\n data[idKey-1] = value;\n var result = <String>[];\n while(data[pointer]!=\'\' ){\n result.add(data[pointer]);\n if( pointer+1 < data.length){ \n pointer++;\n }else{\n break;\n }\n }\n return result;\n\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = OrderedStream(n);\n * List<String> param1 = obj.insert(idKey,value);\n */\n```
1
0
['Array', 'Dart']
0
design-an-ordered-stream
Easy Understandabe java Code~Trust me
easy-understandabe-java-codetrust-me-by-syxw7
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
2012027
NORMAL
2023-01-21T15:12:37.134544+00:00
2023-01-21T15:12:37.134590+00:00
64
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass OrderedStream {\n String[] stream;\n int ptr=0;\n\n\n public OrderedStream(int n) {\n stream=new String[n+1];\n m=n;\n }\n public List<String> insert(int idKey, String value) {\n List<String> list=new ArrayList<>();\n stream[idKey-1]=value;\n while(stream[ptr]!=null)\n {\n list.add(stream[ptr]);\n ptr++;\n }\n return list;\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = new OrderedStream(n);\n * List<String> param_1 = obj.insert(idKey,value);\n */\n```
1
0
['Java']
1
design-an-ordered-stream
JAVA Solution
java-solution-by-ashanvvi-faqs
\n\n# Complexity\n- Time complexity:\nthe time complexity of the code is dominated by the insert() method, which is O(k) where k is the number of elements inser
ashanvvi
NORMAL
2023-01-10T19:50:15.652078+00:00
2023-01-10T19:50:15.652114+00:00
39
false
\n\n# Complexity\n- Time complexity:\nthe time complexity of the code is dominated by the insert() method, which is O(k) where k is the number of elements inserted so far.\n\n- Space complexity:\nThe space complexity of this code is O(n), where n is the number of elements that the stream can hold.\n\n# Code\n```\nclass OrderedStream {\n private String[] data;\n private int ptr;\n\n public OrderedStream(int n) {\n data = new String[n + 1];\n ptr = 1;\n }\n\n public String[] insert(int idKey, String value) {\n data[idKey] = value;\n List<String> res = new ArrayList<>();\n for (int i = ptr; i < data.length; i++) {\n if (data[i] != null) {\n res.add(data[i]);\n } else {\n ptr = i;\n break;\n }\n }\n return res.toArray(new String[0]);\n }\n}\n\n```
1
0
['Java']
0
design-an-ordered-stream
C++ Easy Solution
c-easy-solution-by-_kitish-855z
\nclass OrderedStream {\nprivate:\n vector<string> data;\n int i,x;\npublic:\n OrderedStream(int n) {\n x=n;\n data.resize(n,"");\n
_kitish
NORMAL
2022-12-06T18:20:59.650175+00:00
2022-12-06T18:20:59.650215+00:00
692
false
```\nclass OrderedStream {\nprivate:\n vector<string> data;\n int i,x;\npublic:\n OrderedStream(int n) {\n x=n;\n data.resize(n,"");\n i=1;\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> ans;\n data[idKey-1] = value;\n if(idKey == i){\n for(int j=i-1; j<x; ++j){\n if(!data[j].empty()) ans.push_back(data[j]),i++;\n else break;\n }\n }\n return ans;\n }\n};\n```
1
0
['C++']
0
design-an-ordered-stream
120ms | Go solution
120ms-go-solution-by-kitanoyoru-ikjr
\n\ntype OrderedStream struct {\n hm map[int]string\n n int\n posPtr int \n}\n\nfunc Constructor(n int) OrderedStream {\n return OrderedStream {\n hm: m
kitanoyoru_
NORMAL
2022-11-29T01:23:50.247147+00:00
2022-11-29T01:23:50.247179+00:00
152
false
\n```\ntype OrderedStream struct {\n hm map[int]string\n n int\n posPtr int \n}\n\nfunc Constructor(n int) OrderedStream {\n return OrderedStream {\n hm: make(map[int]string),\n n: n,\n posPtr: 1,\n }\n}\n\n\nfunc (this *OrderedStream) Insert(idKey int, value string) []string {\n this.hm[idKey] = value\n\n ans := []string{}\n for this.posPtr <= this.n && this.hm[this.posPtr] != "" {\n ans = append(ans, this.hm[this.posPtr])\n this.posPtr++\n }\n\n return ans\n}\n```
1
0
['Go']
0
design-an-ordered-stream
c++ | easy | fast
c-easy-fast-by-venomhighs7-h8h4
\n\n# Code\n\n//from vishal_k78\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int maxSize = -1,currIndex = 1;\n \n OrderedStream(int n) {\
venomhighs7
NORMAL
2022-11-22T12:14:45.753891+00:00
2022-11-22T12:14:45.753932+00:00
48
false
\n\n# Code\n```\n//from vishal_k78\nclass OrderedStream {\npublic:\n map<int,string> mp;\n int maxSize = -1,currIndex = 1;\n \n OrderedStream(int n) {\n maxSize = n;\n for(int i=1;i<=n;i++) mp[i] = "----";\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> res;\n mp[idKey] = value;\n int i =1;\n for(int i=currIndex;i<=maxSize;i++){\n if(mp[i] != "----"){\n res.push_back(mp[i]);\n }else{\n currIndex = i;\n break;\n }\n }\n \n return res;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```
1
1
['C++']
0
design-an-ordered-stream
Rust | HashMap
rust-hashmap-by-tomijaga-anv7
rust\nuse std::collections::HashMap;\n\n#[derive(Default)]\nstruct OrderedStream {\n ptr : usize,\n store : HashMap<usize, String>\n}\n\n\nimpl OrderedStr
tomijaga
NORMAL
2022-10-17T03:14:49.166733+00:00
2022-10-17T03:14:49.166773+00:00
67
false
```rust\nuse std::collections::HashMap;\n\n#[derive(Default)]\nstruct OrderedStream {\n ptr : usize,\n store : HashMap<usize, String>\n}\n\n\nimpl OrderedStream {\n\n fn new(n: i32) -> Self {\n Default::default()\n }\n \n fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {\n let id_key = (id_key - 1) as usize;\n self.store.insert(id_key, value);\n \n let mut res = vec![];\n \n if id_key == self.ptr{\n while let Some(val) = self.store.remove(&self.ptr){\n res.push(val);\n self.ptr+=1;\n }\n }\n \n res\n }\n}\n```
1
0
['Rust']
0
design-an-ordered-stream
Java O(N) time O(N) space, with explanation in code
java-on-time-on-space-with-explanation-i-ryma
\n// NOTE: \n// To understand the question, the concatenation of all the chunks should result in a list of the SORTED values.\n// That is why calling the functi
coco007wind
NORMAL
2022-09-14T17:46:05.558918+00:00
2022-09-14T17:46:05.558963+00:00
487
false
```\n// NOTE: \n// To understand the question, the concatenation of all the chunks should result in a list of the SORTED values.\n// That is why calling the function of insert(3, "ccccc") will NOT return anything. \n// In other words, calling insert(1, "ccccc") returns "ccccc"\nclass OrderedStream {\n\n private String[] record;\n\t\n\t//The starting index of the next output when insert() is called\n private int outputIndex; \n\t\n public OrderedStream(int n) {\n // Store current (idKey, Value) into this array\n record = new String[n + 1];\n Arrays.fill(record, "");\n \n // The outputIndex is the starting index of the chunk, which is to be \n // returned via "insert" function. We will need to update outputIndex accordingly\n outputIndex = 1;\n }\n \n public List<String> insert(int idKey, String value) {\n List<String> result = new ArrayList<>();\n \n // Regardless of idKey equals to outputIndex or not, need to store current (idKey, Value)\n record[idKey] = value;\n \n if (idKey == outputIndex) {\n int i = outputIndex;\n while (i < record.length && record[i] != "") {\n result.add(record[i]);\n ++i;\n }\n // outputIndex has scanned the current continuous non-empty value,\n // the next available value is i. \n // Otherwise, it violates "The concatenation of all the chunks should \n // result in a list of the sorted values."\n outputIndex = i;\n }\n return result;\n }\n}\n ```
1
0
[]
0
design-an-ordered-stream
Python3 [runtime faster than 90.82%, memory less than 93.18%] + testing
python3-runtime-faster-than-9082-memory-ufdu0
```\n"""\nexercise: 1656. Design an Ordered Stream\nhttps://leetcode.com/problems/design-an-ordered-stream/\ncompany: Bloomberg\n"""\n\n\n# Your OrderedStream o
kamildoescode
NORMAL
2022-09-02T13:57:03.189384+00:00
2022-09-02T13:57:03.189422+00:00
965
false
```\n"""\nexercise: 1656. Design an Ordered Stream\nhttps://leetcode.com/problems/design-an-ordered-stream/\ncompany: Bloomberg\n"""\n\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.pairs = dict()\n self.pointer = 0\n\n def insert(self, idKey: int, value: str) -> list[str]:\n self.pairs[idKey] = value\n\n temp = list()\n while self.pointer + 1 in self.pairs:\n self.pointer += 1\n\n temp.append(self.pairs[self.pointer])\n\n return temp\n\n\nif __name__ == \'__main__\':\n os = OrderedStream(5)\n\n assert os.insert(3, "ccccc").__eq__([])\n assert os.insert(1, "aaaaa").__eq__([\'aaaaa\'])\n assert os.insert(2, "bbbbb").__eq__(["bbbbb", "ccccc"])\n assert os.insert(5, "eeeee").__eq__([])\n assert os.insert(4, "ddddd").__eq__(["ddddd", "eeeee"])\n
1
0
['Python', 'Python3']
1
design-an-ordered-stream
Rust idiomatic solution
rust-idiomatic-solution-by-pierrewang-fn8a
\nstruct OrderedStream {\n arr: Vec<Option<String>>,\n ptr: usize\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need
pierrewang
NORMAL
2022-08-29T23:53:20.603928+00:00
2022-08-29T23:54:57.626073+00:00
122
false
```\nstruct OrderedStream {\n arr: Vec<Option<String>>,\n ptr: usize\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl OrderedStream {\n\n fn new(n: i32) -> Self {\n Self {\n arr: vec![None; n as usize],\n ptr: 0\n }\n }\n \n fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {\n let id_key = id_key as usize - 1;\n self.arr[id_key] = Some(value);\n \n if id_key > self.ptr {\n return vec![];\n }\n \n while let Some(Some(_)) = self.arr.get(self.ptr) {\n self.ptr += 1;\n }\n self.arr[id_key..self.ptr].iter_mut().map(|opt| opt.take().unwrap()).collect()\n }\n}\n```
1
0
['Rust']
1
design-an-ordered-stream
C++ 💪 Based Solution to terrible problem 💪 with explanation | Beats 150%
c-based-solution-to-terrible-problem-wit-qlzp
\nclass OrderedStream {\nprivate:\n int k; //pointer to min index\n vector<string> chunks; //chunky boy\npublic:\n OrderedStream(int n) {\n k =
rojasdaniel94
NORMAL
2022-08-23T05:23:49.529175+00:00
2022-08-23T05:23:49.529217+00:00
119
false
```\nclass OrderedStream {\nprivate:\n int k; //pointer to min index\n vector<string> chunks; //chunky boy\npublic:\n OrderedStream(int n) {\n k = 1; //init to first pos\n \n for(int i = 0; i < n; i++) {\n chunks.push_back("-1"); //init all pos to -1 (unvisited)\n }\n \n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> chunk{}; //output chonk\n chunks[idKey-1] = value; //replace -1 with actual value, 0-indexed\n \n if(idKey == k) { //if we are on our min pointer\n while(k <= chunks.size() && chunks[k-1] != "-1") { //and we are within bounds and haven\'t visited this index yet, \n chunk.push_back(chunks[k-1]); //push to output chonk\n k++; //increase pointer, will increase to next "-1" unvisited index\n }\n }\n \n return chunk;\n }\n};\n```
1
0
[]
0
design-an-ordered-stream
SIMPLE C++ SOLUTION
simple-c-solution-by-ke4e-ypay
\nclass OrderedStream {\npublic:\n vector<string> s;\n int ptr=1;\n OrderedStream(int n) {\n s.resize(n+1);\n }\n \n vector<string> ins
ke4e
NORMAL
2022-08-21T06:31:59.173211+00:00
2022-08-21T06:31:59.173256+00:00
215
false
```\nclass OrderedStream {\npublic:\n vector<string> s;\n int ptr=1;\n OrderedStream(int n) {\n s.resize(n+1);\n }\n \n vector<string> insert(int idKey, string value) {\n s[idKey]=value;\n vector<string> res;\n while(ptr<s.size() && !s[ptr].empty())\n res.push_back(s[ptr++]);\n return res;\n }\n};\n```
1
0
['C']
0
design-an-ordered-stream
c++ easy straightforward
c-easy-straightforward-by-colorfulpencil-fuzh
\nclass OrderedStream {\npublic:\n \n vector<string> orderedStream; \n \n int start;\n \n OrderedStream(int n) {\n for(int i=0; i<n; i+
colorfulpencil
NORMAL
2022-08-20T15:38:51.581705+00:00
2022-08-20T15:38:51.581736+00:00
338
false
```\nclass OrderedStream {\npublic:\n \n vector<string> orderedStream; \n \n int start;\n \n OrderedStream(int n) {\n for(int i=0; i<n; i++){\n orderedStream.push_back("");\n }\n \n start = 0;\n }\n \n vector<string> insert(int idKey, string value) {\n orderedStream[idKey-1] = value; \n \n vector<string> ans;\n \n for(int i=start; i<orderedStream.size(); i++){\n if(orderedStream[i] != ""){\n start++; \n ans.push_back(orderedStream[i]);\n }\n else{\n return ans;\n }\n }\n \n return ans;\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n ```
1
0
['C', 'C++']
0
design-an-ordered-stream
C++ || Easy || Global pointer
c-easy-global-pointer-by-user3405ye-djpp
We just need a global pointer to store the last index till which the elements have been printed or sent to the output stream. The only way to do it (approach th
user3405Ye
NORMAL
2022-07-18T18:42:56.049713+00:00
2022-07-18T18:42:56.049757+00:00
178
false
We just need a **global pointer** to store the **last index** till which the elements have been printed or sent to the output stream. The only way to do it (approach the problem) is in a linear fashion, from the starting index. Hence, the logic of the solution.\n**Please upvote if you find the solution good.\n*Thank You!!***\n```\nclass OrderedStream {\npublic:\n int lastval = -1, n;\n vector<string> v;\n OrderedStream(int n) {\n v = vector<string>(n);\n this->n = n;\n }\n \n vector<string> insert(int idKey, string value) {\n v[idKey-1] = value;\n if(idKey - 1 - lastval == 1) {\n int idx = idKey-1;\n vector<string> temp;\n while(idx < n and v[idx].size()) temp.push_back(v[idx++]);\n lastval = idx-1;\n return temp;\n }\n return {};\n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n ```
1
0
['C', 'C++']
0
design-an-ordered-stream
c++ || Easy
c-easy-by-aabargaje-u90l
\nclass OrderedStream {\npublic:\n\n\t// Keep track how many values we have returned alredy.\n int startIndex = 0;\n vector<string> val;\n \n Ordere
aabargaje
NORMAL
2022-07-09T04:59:31.272148+00:00
2022-07-09T04:59:31.272191+00:00
137
false
```\nclass OrderedStream {\npublic:\n\n\t// Keep track how many values we have returned alredy.\n int startIndex = 0;\n vector<string> val;\n \n OrderedStream(int n) {\n val.resize(n+1,"");\n }\n \n vector<string> insert(int idKey, string value) {\n vector<string> ans;\n val[idKey - 1] = value;\n \n\t\t// Push values to answer vector until value is not NULL or EMPTY\n while(val[startIndex] != ""){\n ans.push_back(val[startIndex]);\n startIndex++;\n }\n \n return ans;\n }\n};\n\n```
1
0
['C']
0
design-an-ordered-stream
Reverse engineered solution to understand the problem LOL
reverse-engineered-solution-to-understan-3ufx
The problem description is so unclear that to finally understand what the heck author is asking for I had to find some solution here on discussion and reverse e
trpaslik
NORMAL
2022-05-19T17:30:50.706433+00:00
2022-05-19T17:30:50.706461+00:00
110
false
The problem description is so unclear that to finally understand **what the heck author is asking for** I had to find some solution here on discussion and reverse engineer it back to the problem description. \n\nDo you guys also find the unstoppable animated gif so annoying?\n\nThis is surly the most frustraifing "easy" so far...
1
0
[]
1
design-an-ordered-stream
[C++] elegant solution
c-elegant-solution-by-kristijan1996-vq4r
It is quite hard to understand the requirements at first, but the animation makes it easier.\nSo, we know in advance that we will be having an array of strings
kristijan1996
NORMAL
2022-05-10T17:19:11.326187+00:00
2022-05-21T08:01:29.476309+00:00
215
false
It is quite hard to understand the requirements at first, but the animation makes it easier.\nSo, we know in advance that we will be having an array of strings of length *n*, and therefore\nthe constructor allocates a vector of empty strings. That is quite easy.\n\nInserting a value is also easy, since it is guaranteed, by the problem description, that each pair\nwill have a unique *id*. You just replace the empty string on a position given by *idKey* (-1, since\nwe count from 0) with string *value*.\n\nNow comes the tricky part. Upon each insertion, you need to return a list of strings such that it\nis a continuos chunk, with no empty strings between them. You start off from index *lastIndex*, and in a \naux buffer you *push_back* all non-empty strings that come afterwards, and you increment *lastIndex*\nafter each push. Also, pay attention that you cannot check *arr.at(lastIndex)* if *lastIndex* is out of range.\nThat will throw you a runtime error which is hard to figure out. That\'s why you need *lastIndex < arr.size()*.\n\nNo dynamic allocation, so no destructor is needed.\n\n\n```\nclass OrderedStream {\npublic:\n OrderedStream(int n) : arr {vector<string>(n, "")} \n {\n }\n \n vector<string> insert(int idKey, string value) \n {\n arr.at(idKey-1) = value; \n \n vector<string> ret{};\n \n while (lastIndex < arr.size() && arr.at(lastIndex) != "")\n {\n ret.push_back(arr.at(lastIndex++));\n }\n \n return ret;\n }\n \nprivate:\n vector<string> arr;\n int lastIndex{0};\n};\n```\n\nThanks to [@RedaKerouicha](https://leetcode.com/RedaKerouicha) for pointing out that *vector<string> arr{};* should be *vector<string> arr;* since we initialize it in the constructor.\n
1
0
['C', 'C++']
1
design-an-ordered-stream
c++ | Array | easy
c-array-easy-by-srv-er-se1h
\nclass OrderedStream {\npublic:\n vector<string>v;\n int id=1;\n OrderedStream(int n) {\n v=vector<string>(n+1,"");\n }\n \n vector<st
srv-er
NORMAL
2022-04-21T04:50:54.791218+00:00
2022-04-21T04:50:54.791260+00:00
164
false
```\nclass OrderedStream {\npublic:\n vector<string>v;\n int id=1;\n OrderedStream(int n) {\n v=vector<string>(n+1,"");\n }\n \n vector<string> insert(int idKey, string value) {\n v[idKey]=value;\n vector<string> ans;\n while(id<v.size() && v[id]!="")ans.push_back(v[id++]);\n return ans; \n }\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(idKey,value);\n */\n```
1
0
['Array', 'C']
1
design-an-ordered-stream
[C++/Python] vector based hashmap & global tracking index
cpython-vector-based-hashmap-global-trac-hztz
[C++/Python] vector based hashmap & global tracking index\n\nApproach 1: vector based hashmap + global tracking index (C++ version)\n\nclass OrderedStream { //
codedayday
NORMAL
2022-04-15T07:29:35.916731+00:00
2022-04-15T07:29:35.916766+00:00
130
false
[C++/Python] vector based hashmap & global tracking index\n\nApproach 1: vector based hashmap + global tracking index (C++ version)\n```\nclass OrderedStream { // BEST: use global index to track order 1~n\npublic:\n OrderedStream(int n):_vs(n+1), _idx(1) { }\n \n vector<string> insert(int idKey, string value) {\n _vs[idKey]=value;\n vector<string> ans;\n while(_idx< _vs.size() && !_vs[_idx].empty())\n ans.push_back(_vs[_idx++]);\n return ans;\n }\nprivate:\n vector<string> _vs;\n int _idx;\n};\n```\n\nApproach 1: Python\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self._vs = [None]*(n+1)\n self._ptr = 1\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self._vs[idKey] = value\n ans = []\n while self._ptr < len(self._vs) and _vs[self._ptr]:\n ans.append(_vs[self._ptr])\n self._ptr+=1\n \n return ans\n```
1
0
['C', 'Python']
0
design-an-ordered-stream
Simple python solution. Faster than 99.28%
simple-python-solution-faster-than-9928-m4flg
\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.start = 1\n self.ls = [None]*(n+1)\n\n def insert(self, id
amitbansal13
NORMAL
2022-03-06T17:19:34.197775+00:00
2022-03-06T17:19:34.197855+00:00
65
false
```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.start = 1\n self.ls = [None]*(n+1)\n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.ls[idKey] = value\n if self.ls[self.start] is None:\n return []\n \n ans = []\n \n while self.start <= self.n and self.ls[self.start] is not None:\n ans.append(self.ls[self.start])\n self.start+=1\n \n return ans\n\n# Your OrderedStream object will be instantiated and called as such:\n# obj = OrderedStream(n)\n# param_1 = obj.insert(idKey,value)\n```\n\nIf there are any questions do comment
1
0
[]
0
design-an-ordered-stream
Java Solution With Explanation
java-solution-with-explanation-by-am0611-0cxd
\nclass OrderedStream {\n Map<Integer, String> stream;\n int ptr; // intialize ptr, then find the longest contiguous increasing sequence of ids from the p
am0611
NORMAL
2022-02-21T16:32:07.001751+00:00
2022-02-21T16:35:24.648274+00:00
230
false
```\nclass OrderedStream {\n Map<Integer, String> stream;\n int ptr; // intialize ptr, then find the longest contiguous increasing sequence of ids from the ptr\n public OrderedStream(int n) {\n this.stream = new HashMap<>();\n this.ptr = 1;\n }\n \n public List<String> insert(int idKey, String value) {\n stream.put(idKey, value);\n \n List<String> result = new ArrayList<>();\n \n\t\t// if the id which the ptr is currently pointing to, is populated, find the longest contiguous sequence of data (chunks) available, else return empty list\n while(stream.containsKey(ptr))\n {\n\t\t//increment the ptr to next id until we reach an id that is not yet populated. Next time we\'ll start reading data from that id if it gets populated.\n result.add(stream.get(ptr++));\n }\n \n return result;\n }\n}\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream obj = new OrderedStream(n);\n * List<String> param_1 = obj.insert(idKey,value);\n */\n```
1
0
['Java']
0
design-an-ordered-stream
Java, HashMap solution.
java-hashmap-solution-by-botir_adoshboye-3m5s
\nclass OrderedStream {\n\n Map<Integer, String> store;\n int lastIndex = 0;\n\n public OrderedStream(int n) {\n store = new HashMap<>(n);\n
botir_adoshboyev
NORMAL
2022-02-15T11:30:19.273604+00:00
2022-02-15T11:30:19.273633+00:00
185
false
```\nclass OrderedStream {\n\n Map<Integer, String> store;\n int lastIndex = 0;\n\n public OrderedStream(int n) {\n store = new HashMap<>(n);\n }\n\n public List<String> insert(int idKey, String value) {\n List<String> res = new ArrayList<>();\n store.put(idKey, value);\n while(store.containsKey(lastIndex+1)) {\n res.add(store.get(++lastIndex));\n }\n return res;\n }\n}\n```
1
0
['Java']
0
design-an-ordered-stream
Cpp Solution
cpp-solution-by-rautela-rse2
\nclass OrderedStream {\n vector<string> strm;\n int cptr = 0;\npublic:\n OrderedStream(int n) {\n strm.resize(n);\n }\n \n vector<stri
rautela
NORMAL
2022-02-13T09:31:58.012525+00:00
2022-02-13T09:31:58.012557+00:00
55
false
```\nclass OrderedStream {\n vector<string> strm;\n int cptr = 0;\npublic:\n OrderedStream(int n) {\n strm.resize(n);\n }\n \n vector<string> insert(int idKey, string value) {\n strm[idKey - 1] = value;\n vector<string> rstrm;\n while(cptr < strm.size() and strm[cptr] != "")\n rstrm.push_back(strm[cptr++]);\n return rstrm;\n }\n};\n\n```
1
0
[]
0
design-an-ordered-stream
[Python] Design an Ordered Stream
python-design-an-ordered-stream-by-a_m_r-ck71
I just need help with the design , if it can be improved.\n\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.dp = {i
a_m_rohit
NORMAL
2022-02-06T13:39:42.469176+00:00
2022-02-06T13:39:42.469210+00:00
154
false
I just need help with the design , if it can be improved.\n\n```\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.n = n\n self.dp = {i:[] for i in range(1,self.n+1)}\n self.i = 1\n\n def insert(self, idKey: int, value: str) -> List[str]:\n (self.dp[idKey]).insert(0,value)\n ss= []\n if len(self.dp[self.i]) == 0:\n return []\n while self.i<= self.n and len(self.dp[self.i]) != 0 :\n ss+=(self.dp[self.i])\n self.i += 1\n return ss\n\n```
1
1
[]
0
design-an-ordered-stream
Rust solution with HashMap
rust-solution-with-hashmap-by-kuskus87-ok3o
Since there is only one rust-tagged solution for this problem, I will post another one which is \ntaken from here https://rustgym.com/leetcode/1656\n\nrust\nuse
kuskus87
NORMAL
2022-02-01T10:27:17.525344+00:00
2022-02-01T10:27:17.525386+00:00
122
false
Since there is only one rust-tagged solution for this problem, I will post another one which is \ntaken from here https://rustgym.com/leetcode/1656\n\n```rust\nuse std::collections::HashMap;\n\nstruct OrderedStream {\n kv: HashMap<i32, String>,\n n: i32, \n i: i32,\n}\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl OrderedStream {\n\n fn new(n: i32) -> Self {\n OrderedStream {\n kv: HashMap::new(),\n n: n,\n i: 1\n }\n }\n \n fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {\n self.kv.insert(id_key, value);\n let mut res: Vec<String> = Vec::new();\n if self.i == id_key {\n for j in id_key..=self.n {\n if let Some(v) = self.kv.get(&j) {\n res.push(v.to_string());\n self.i += 1;\n } else {\n break;\n }\n }\n }\n return res;\n }\n}\n```
1
0
['Rust']
0
design-an-ordered-stream
Python solution (based on heap)
python-solution-based-on-heap-by-labdmit-imwy
\nimport heapq\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.idx = 1\n self.heap = []\n\n def insert(self, idKey: int, valu
labdmitriy
NORMAL
2022-01-05T18:09:58.118918+00:00
2022-01-05T18:09:58.118965+00:00
223
false
```\nimport heapq\n\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.idx = 1\n self.heap = []\n\n def insert(self, idKey: int, value: str) -> List[str]:\n result = []\n \n if idKey == self.idx:\n result.append(value)\n self.idx += 1\n \n while self.heap and self.heap[0][0] == self.idx:\n result.append(heapq.heappop(self.heap)[1])\n self.idx += 1\n else:\n heapq.heappush(self.heap, (idKey, value))\n \n return result\n```
1
0
['Heap (Priority Queue)', 'Python']
1
design-an-ordered-stream
python3 solution
python3-solution-by-peter-pan-98fk
python\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.index=0\n self.lt=[None]*n\n def insert(self, idKey: int, value: str) ->
peter-pan
NORMAL
2022-01-03T01:52:00.744063+00:00
2022-01-03T01:52:00.744102+00:00
149
false
```python\nclass OrderedStream:\n\n def __init__(self, n: int):\n self.index=0\n self.lt=[None]*n\n def insert(self, idKey: int, value: str) -> List[str]:\n self.lt[idKey-1]=value\n if idKey!=self.index+1:\n return []\n num=0\n output=list()\n for ele in self.lt[self.index:]:\n if ele is None:\n break\n output.append(ele)\n num+=1\n self.index+=num\n return output\n```
1
0
[]
0
design-an-ordered-stream
Java || Explained the confusing description || Clean Solution
java-explained-the-confusing-description-j6un
// Confusing descriptoin. Here is what I understood from the GIF\n// when insert() is called, return the longest list that starts at index of pointer. If there
ICodeToSurvive
NORMAL
2021-12-27T22:36:10.015899+00:00
2021-12-27T22:36:10.015946+00:00
198
false
// Confusing descriptoin. Here is what I understood from the GIF\n// when insert() is called, return the longest list that starts at index of pointer. If there is not an elemnt at pointer(start), then return empty list\n\n// The POINTER is the next START window\n\n// TC : O(N) -> avg run time -> since we iterate the array once\n// SC : O(N) -> to store keys in worst case\n```\nclass OrderedStream {\n\n private String[] arr;\n private int currentPtr;\n \n public OrderedStream(int n) {\n this.arr = new String[n];\n this.currentPtr = 0;\n }\n \n public List<String> insert(int idKey, String value) {\n arr[idKey-1] = value;\n \n List<String> result = new ArrayList<>();\n for(int i = currentPtr; i < arr.length; i++) { // run only starts from pointer, we do not need to start from 0\n if(arr[i] == null) break; // if there is no element, this breaks the chain so come out of the for loop\n result.add(arr[i]);\n currentPtr++;\n }\n \n return result;\n }\n}\n```
1
0
[]
0
design-an-ordered-stream
C++, vector
c-vector-by-aleksej2-6xfy
\nclass OrderedStream {\n vector<string> data;\n int N;\n int ptr;\npublic:\n OrderedStream(int n) : data(n), N{n}, ptr{1} {\n }\n \n vecto
aleksej2
NORMAL
2021-12-14T14:10:46.235086+00:00
2021-12-14T14:10:46.235125+00:00
125
false
```\nclass OrderedStream {\n vector<string> data;\n int N;\n int ptr;\npublic:\n OrderedStream(int n) : data(n), N{n}, ptr{1} {\n }\n \n vector<string> insert(int id, string value) {\n data[id-1] = move(value);\n vector<string> ret;\n if(ptr != id)\n return ret;\n while(ptr <= N) {\n if(data[ptr-1] == "")\n break;\n ret.emplace_back(data[ptr-1]);\n ++ptr;\n }\n return ret;\n }\n};\n```
1
0
[]
0
design-an-ordered-stream
C++ | T: O(n), S: O(n) | Vector approach
c-t-on-s-on-vector-approach-by-snowspire-ennp
\nclass OrderedStream final {\npublic:\n explicit OrderedStream() noexcept = default;\n explicit OrderedStream(const OrderedStream&) noexcept = default;\n
snowspire_
NORMAL
2021-11-30T20:46:33.007370+00:00
2021-11-30T20:46:33.007407+00:00
123
false
```\nclass OrderedStream final {\npublic:\n explicit OrderedStream() noexcept = default;\n explicit OrderedStream(const OrderedStream&) noexcept = default;\n\n OrderedStream(int n) : stream_(std::vector<std::string>(n, "")), current_index_(0) {}\n\n std::vector<std::string> insert(int id, std::string value) {\n std::vector<std::string> response;\n stream_[id - 1] = std::move(value);\n\n for (; current_index_ < stream_.size(); ++current_index_) {\n if (!stream_[current_index_].empty()) {\n response.push_back(stream_[current_index_]);\n } else {\n break;\n }\n }\n\n return response;\n }\n\nprivate:\n std::vector<std::string> stream_;\n int current_index_;\n};\n\n/**\n * Your OrderedStream object will be instantiated and called as such:\n * OrderedStream* obj = new OrderedStream(n);\n * vector<string> param_1 = obj->insert(id,value);\n */\n\n```
1
0
[]
0