forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1656.java
41 lines (37 loc) · 1.16 KB
/
_1656.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class _1656 {
public static class Solution1 {
public static class OrderedStream {
TreeMap<Integer, String> map;
int ptr;
int limit;
public OrderedStream(int n) {
this.map = new TreeMap<>();
this.ptr = 1;
this.limit = n;
}
public List<String> insert(int id, String value) {
map.put(id, value);
List<String> result = new ArrayList<>();
if (map.containsKey(ptr)) {
for (int key = ptr; key <= limit; key++) {
if (map.containsKey(key)) {
result.add(map.get(key));
} else {
break;
}
}
int i = id;
while (map.containsKey(i)) {
i++;
}
ptr = i;
}
return result;
}
}
}
}