-
Notifications
You must be signed in to change notification settings - Fork 0
/
1656_design_an_ordered_stream.go
52 lines (43 loc) · 1.17 KB
/
1656_design_an_ordered_stream.go
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
42
43
44
45
46
47
48
49
50
51
52
package easy
/*
https://leetcode.com/problems/design-an-ordered-stream/
Constraints:
1 <= n <= 1000
1 <= id <= n
value.length == 5
value consists only of lowercase letters.
Each call to insert will have a unique id.
Exactly n calls will be made to insert.
*/
/*
Runtime: 69 ms, faster than 96.19% of Go online submissions for Design an Ordered Stream.
Memory Usage: 7.2 MB, less than 88.57% of Go online submissions for Design an Ordered Stream.
*/
type OrderedStream struct {
last int
values []string
}
//Constructor renamed -> ConstructorOrderedStream
func ConstructorOrderedStream(n int) OrderedStream {
stream := OrderedStream{}
stream.values = make([]string, n+1)
return stream
}
//Insert renamed -> InsertOrderedStream
func (this *OrderedStream) InsertOrderedStream(idKey int, value string) []string {
this.values[idKey-1] = value
result := make([]string, 0)
for i := this.last; i < len(this.values); i++ {
if this.values[i] == "" {
break
}
result = append(result, this.values[i])
this.last = i + 1
}
return result
}
/**
* Your OrderedStream object will be instantiated and called as such:
* obj := Constructor(n);
* param_1 := obj.Insert(idKey,value);
*/