Skip to content

Commit ca1fcd8

Browse files
add 1656
1 parent 41b4b16 commit ca1fcd8

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
1112
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
1213
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
1314
|1637|[Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)|[Javascript](./javascript/_1637.js)| | Medium | Sort |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
7+
public class _1656 {
8+
public static class Solution1 {
9+
public static class OrderedStream {
10+
TreeMap<Integer, String> map;
11+
int ptr;
12+
int limit;
13+
14+
public OrderedStream(int n) {
15+
this.map = new TreeMap<>();
16+
this.ptr = 1;
17+
this.limit = n;
18+
}
19+
20+
public List<String> insert(int id, String value) {
21+
map.put(id, value);
22+
List<String> result = new ArrayList<>();
23+
if (map.containsKey(ptr)) {
24+
for (int key = ptr; key <= limit; key++) {
25+
if (map.containsKey(key)) {
26+
result.add(map.get(key));
27+
} else {
28+
break;
29+
}
30+
}
31+
int i = id;
32+
while (map.containsKey(i)) {
33+
i++;
34+
}
35+
ptr = i;
36+
}
37+
return result;
38+
}
39+
}
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1656;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1656Test {
14+
private static _1656.Solution1.OrderedStream orderedStream;
15+
16+
@Test
17+
public void test1() {
18+
orderedStream = new _1656.Solution1.OrderedStream(5);
19+
assertEquals(Collections.emptyList(), orderedStream.insert(3, "ccccc"));
20+
assertEquals(Arrays.asList("aaaaa"), orderedStream.insert(1, "aaaaa"));
21+
assertEquals(Arrays.asList("bbbbb", "ccccc"), orderedStream.insert(2, "bbbbb"));
22+
assertEquals(Collections.emptyList(), orderedStream.insert(5, "eeeee"));
23+
assertEquals(Arrays.asList("ddddd", "eeeee"), orderedStream.insert(4, "ddddd"));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)