We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5844a58 commit fc7f460Copy full SHA for fc7f460
C++/design-an-ordered-stream.cpp
@@ -0,0 +1,28 @@
1
+// Time: O(1), amortized
2
+// Space: O(n)
3
+
4
+class OrderedStream {
5
+public:
6
+ OrderedStream(int n)
7
+ : i_(0)
8
+ , values_(n) {
9
10
+ }
11
12
+ vector<string> insert(int id, string value) {
13
+ --id;
14
+ values_[id] = value;
15
+ vector<string> result;
16
+ if (id != i_) {
17
+ return result;
18
19
+ while (i_ < size(values_) && !empty(values_[i_])) {
20
+ result.emplace_back(values_[i_++]);
21
22
23
24
25
+private:
26
+ int i_;
27
+ vector<string> values_;
28
+};
0 commit comments