Skip to content

Commit fc7f460

Browse files
authored
Create design-an-ordered-stream.cpp
1 parent 5844a58 commit fc7f460

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/design-an-ordered-stream.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return result;
23+
}
24+
25+
private:
26+
int i_;
27+
vector<string> values_;
28+
};

0 commit comments

Comments
 (0)