Skip to content

Commit 6302f8c

Browse files
committed
1656. Design an Ordered Stream - final
1 parent 25d67b8 commit 6302f8c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ood.design;
2+
3+
import annotation.Platform;
4+
import annotation.Quality;
5+
import annotation.Site;
6+
import annotation.Stage;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* 1656. Design an Ordered Stream
15+
* */
16+
@Quality(Stage.TESTED)
17+
@Platform(Site.LEETCODE)
18+
public class OrderedStream {
19+
20+
private int size,pointer;
21+
private int[] values;
22+
private Map<Integer,String> map;
23+
24+
public OrderedStream(int n) {
25+
size = n;
26+
pointer = 1;
27+
values = new int[n+1];
28+
map = new HashMap<>();
29+
}
30+
31+
public List<String> insert(int idKey, String value) {
32+
values[idKey] = idKey;
33+
map.put(idKey,value);
34+
return getStreamValues();
35+
}
36+
37+
private List<String> getStreamValues(){
38+
List<String> result = new ArrayList<>();
39+
if(values[pointer] == 0)
40+
return result;
41+
for(int i = pointer; i <= size ; i++) {
42+
if(map.containsKey(i)){
43+
result.add(map.get(i));
44+
} else {
45+
pointer = i;
46+
break;
47+
}
48+
}
49+
return result;
50+
}
51+
52+
public static void main(String[] args){
53+
OrderedStream od = new OrderedStream(5);
54+
od.insert(3,"ccccc");
55+
od.insert(1,"aaaaa");
56+
od.insert(2,"bbbbb");
57+
od.insert(5,"eeeee");
58+
59+
System.out.println(
60+
od.insert(4,"ddddd")
61+
);
62+
}
63+
}

0 commit comments

Comments
 (0)