Skip to content

Commit 641e19b

Browse files
committed
Add solution #1656
1 parent 57ae428 commit 641e19b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,454 LeetCode solutions in JavaScript
1+
# 1,455 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1276,6 +1276,7 @@
12761276
1653|[Minimum Deletions to Make String Balanced](./solutions/1653-minimum-deletions-to-make-string-balanced.js)|Medium|
12771277
1654|[Minimum Jumps to Reach Home](./solutions/1654-minimum-jumps-to-reach-home.js)|Medium|
12781278
1655|[Distribute Repeating Integers](./solutions/1655-distribute-repeating-integers.js)|Hard|
1279+
1656|[Design an Ordered Stream](./solutions/1656-design-an-ordered-stream.js)|Easy|
12791280
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12801281
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12811282
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1656. Design an Ordered Stream
3+
* https://leetcode.com/problems/design-an-ordered-stream/
4+
* Difficulty: Easy
5+
*
6+
* There is a stream of n (idKey, value) pairs arriving in an arbitrary order, where idKey is
7+
* an integer between 1 and n and value is a string. No two pairs have the same id.
8+
*
9+
* Design a stream that returns the values in increasing order of their IDs by returning a chunk
10+
* (list) of values after each insertion. The concatenation of all the chunks should result in a
11+
* list of the sorted values.
12+
*
13+
* Implement the OrderedStream class:
14+
* - OrderedStream(int n) Constructs the stream to take n values.
15+
* - String[] insert(int idKey, String value) Inserts the pair (idKey, value) into the stream,
16+
* then returns the largest possible chunk of currently inserted values that appear next in
17+
* the order.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
*/
23+
var OrderedStream = function(n) {
24+
this.stream = new Array(n).fill(null);
25+
this.nextIndex = 0;
26+
};
27+
28+
/**
29+
* @param {number} idKey
30+
* @param {string} value
31+
* @return {string[]}
32+
*/
33+
OrderedStream.prototype.insert = function(idKey, value) {
34+
const index = idKey - 1;
35+
this.stream[index] = value;
36+
37+
if (index !== this.nextIndex) return [];
38+
39+
const result = [];
40+
while (this.nextIndex < this.stream.length && this.stream[this.nextIndex] !== null) {
41+
result.push(this.stream[this.nextIndex]);
42+
this.nextIndex++;
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)