1656. Design an Ordered Stream Easy
1/**
2 * [1656] Design an Ordered Stream
3 *
4 * There is a stream of n (idKey, value) pairs arriving in an arbitrary order, where idKey is an integer between 1 and n and value is a string. No two pairs have the same id.
5 * Design a stream that returns the values in increasing order of their IDs by returning a chunk (list) of values after each insertion. The concatenation of all the chunks should result in a list of the sorted values.
6 * Implement the OrderedStream class:
7 *
8 * OrderedStream(int n) Constructs the stream to take n values.
9 * String[] insert(int idKey, String value) Inserts the pair (idKey, value) into the stream, then returns the largest possible chunk of currently inserted values that appear next in the order.
10 *
11 *
12 * Example:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/10/q1.gif" style="width: 682px; height: 240px;" />
14 *
15 * Input
16 * ["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
17 * [[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
18 * Output
19 * [null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
20 * Explanation
21 * // Note that the values ordered by ID is ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"].
22 * OrderedStream os = new OrderedStream(5);
23 * os.insert(3, "ccccc"); // Inserts (3, "ccccc"), returns [].
24 * os.insert(1, "aaaaa"); // Inserts (1, "aaaaa"), returns ["aaaaa"].
25 * os.insert(2, "bbbbb"); // Inserts (2, "bbbbb"), returns ["bbbbb", "ccccc"].
26 * os.insert(5, "eeeee"); // Inserts (5, "eeeee"), returns [].
27 * os.insert(4, "ddddd"); // Inserts (4, "ddddd"), returns ["ddddd", "eeeee"].
28 * // Concatentating all the chunks returned:
29 * // [] + ["aaaaa"] + ["bbbbb", "ccccc"] + [] + ["ddddd", "eeeee"] = ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"]
30 * // The resulting order is the same as the order above.
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= n <= 1000
36 * 1 <= id <= n
37 * value.length == 5
38 * value consists only of lowercase letters.
39 * Each call to insert will have a unique id.
40 * Exactly n calls will be made to insert.
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/design-an-ordered-stream/
46// discuss: https://leetcode.com/problems/design-an-ordered-stream/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50struct OrderedStream {
51 false
52 }
53
54
55/**
56 * `&self` means the method takes an immutable reference.
57 * If you need a mutable reference, change it to `&mut self` instead.
58 */
59impl OrderedStream {
60
61 fn new(n: i32) -> Self {
62
63 }
64
65 fn insert(&self, id_key: i32, value: String) -> Vec<String> {
66
67 }
68}
69
70/**
71 * Your OrderedStream object will be instantiated and called as such:
72 * let obj = OrderedStream::new(n);
73 * let ret_1: Vec<String> = obj.insert(idKey, value);
74 */
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_1656() {
84 }
85}
86
Back
© 2025 bowen.ge All Rights Reserved.