-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path635-DesignLogStorageSystem.go
More file actions
290 lines (264 loc) · 10 KB
/
635-DesignLogStorageSystem.go
File metadata and controls
290 lines (264 loc) · 10 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
// 635. Design Log Storage System
// You are given several logs, where each log contains a unique ID and timestamp.
// Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second,
// for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers.
// Implement the LogSystem class:
// LogSystem() Initializes the LogSystem object.
// void put(int id, string timestamp) Stores the given log (id, timestamp) in your storage system.
// int[] retrieve(string start, string end, string granularity) Returns the IDs of the logs whose timestamps are within the range from start to end inclusive. start and end all have the same format as timestamp, and granularity means how precise the range should be (i.e. to the exact Day, Minute, etc.). For example, start = "2017:01:01:23:59:59", end = "2017:01:02:23:59:59", and granularity = "Day" means that we need to find the logs within the inclusive range from Jan. 1st 2017 to Jan. 2nd 2017, and the Hour, Minute, and Second for each log entry can be ignored.
// Example 1:
// Input
// ["LogSystem", "put", "put", "put", "retrieve", "retrieve"]
// [[], [1, "2017:01:01:23:59:59"], [2, "2017:01:01:22:59:59"], [3, "2016:01:01:00:00:00"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour"]]
// Output
// [null, null, null, null, [3, 2, 1], [2, 1]]
// Explanation
// LogSystem logSystem = new LogSystem();
// logSystem.put(1, "2017:01:01:23:59:59");
// logSystem.put(2, "2017:01:01:22:59:59");
// logSystem.put(3, "2016:01:01:00:00:00");
// // return [3,2,1], because you need to return all logs between 2016 and 2017.
// logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year");
// // return [2,1], because you need to return all logs between Jan. 1, 2016 01:XX:XX and Jan. 1, 2017 23:XX:XX.
// // Log 3 is not returned because Jan. 1, 2016 00:00:00 comes before the start of the range.
// logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour");
// Constraints:
// 1 <= id <= 500
// 2000 <= Year <= 2017
// 1 <= Month <= 12
// 1 <= Day <= 31
// 0 <= Hour <= 23
// 0 <= Minute, Second <= 59
// granularity is one of the values ["Year", "Month", "Day", "Hour", "Minute", "Second"].
// At most 500 calls will be made to put and retrieve.
import "fmt"
import "strings"
type LogSystem struct {
logs []Log
indexMap map[string]int
}
type Log struct {
timestamp string
id int
}
func Constructor() LogSystem {
indexMap := map[string]int{}
indexMap["Year"] = 1
indexMap["Month"] = 2
indexMap["Day"] = 3
indexMap["Hour"] = 4
indexMap["Minute"] = 5
indexMap["Second"] = 6
return LogSystem{
logs: make([]Log, 0),
indexMap: indexMap,
}
}
// 利用二分查找,查找插入位置
func (this *LogSystem) Put(id int, timestamp string) {
// 根据输入的数据,产生一条新日志
log := Log{
timestamp: timestamp,
id: id,
}
// 二分查找,查找插入位置,队列是根据 timestamp 升序排列的
low, high := 0, len(this.logs)-1
pos := -1
for low <= high {
mid := (low + high) / 2
compareRes := strings.Compare(timestamp, this.logs[mid].timestamp)
if compareRes == 0 { // 待插入元素等于中间位置,可插入中间位置的下一个位置,跳出循环
pos = mid
return // 重复的不允许插入 65 过不了
// break
} else if compareRes < 0 { // 待插入元素小于中间位置,需要在左侧插入
high = mid - 1
} else if compareRes > 0 { // 待插入元素大于中间位置,需要在右侧插入
low = mid + 1
}
}
if pos == -1 { // 没有找到等于 timestamp 的元素,此时 low = high + 1
pos = high
}
newLogs := make([]Log, len(this.logs)+1)
// put new log in position pos+1
copy(newLogs[0:pos+1], this.logs[0:pos+1])
newLogs[pos+1] = log
copy(newLogs[pos+2:], this.logs[pos+1:])
this.logs = newLogs
}
func (this *LogSystem) Retrieve(start string, end string, granularity string) []int {
pos := this.Search(start, granularity)
ids := []int{}
for i := pos; i < len(this.logs); i++ {
if this.compare(this.logs[i].timestamp, start, granularity) >= 0 &&
this.compare(this.logs[i].timestamp, end, granularity) <= 0 {
ids = append(ids, this.logs[i].id)
}
}
return ids
}
// search the first position i, i >= start
func (this *LogSystem) Search(start string, granularity string) int {
l, h := 0, len(this.logs)-1
pos := -1
for l <= h {
mid := (l + h) / 2
cmpRes := this.compare(start, this.logs[mid].timestamp, granularity)
if cmpRes == 0 { // start == mid
pos = mid
break
} else if cmpRes > 0 {
l = mid + 1
} else {
h = mid - 1
}
}
if pos == -1 {
return h + 1
}
for pos >= 0 && this.compare(start, this.logs[pos].timestamp, granularity) == 0 {
pos--
}
return pos + 1
}
// 2016:01:01:01:01:01
// "Year", "Month", "Day", "Hour", "Minute", "Second"
func (this *LogSystem) compare(a, b string, granularity string) int {
ta, tb := strings.Split(a, ":"), strings.Split(b, ":")
index := this.indexMap[granularity]
cptimea := strings.Join(ta[0:index], ":")
cptimeb := strings.Join(tb[0:index], ":")
return strings.Compare(cptimea, cptimeb)
}
// import "sort"
// type Time struct {
// val string
// id int
// }
// type LogSystem struct {
// times []*Time
// id2t map[int]*Time
// }
// func Constructor() LogSystem {
// times := []*Time{}
// id2t := make(map[int]*Time)
// return LogSystem{times, id2t}
// }
// func (this *LogSystem) Put(id int, timestamp string) {
// if _, ok := this.id2t[id]; ok {
// return
// }
// nt := &Time{timestamp, id}
// this.times = append(this.times, nt)
// this.id2t[id] = nt
// sort.Slice(this.times, func(i, j int) bool {
// return this.times[i].val < this.times[j].val
// })
// }
// func (this *LogSystem) Retrieve(start string, end string, granularity string) []int {
// res := []int{}
// if granularity == "Second" {
// startIndex := sort.Search(len(this.times), func(i int) bool {
// return this.times[i].val >= start
// })
// endIndex := sort.Search(len(this.times), func(i int) bool {
// return this.times[i].val > end
// })
// for i := startIndex; i < endIndex; i++ {
// res = append(res, this.times[i].id)
// }
// return res
// }
// timeLen := len(start)
// if granularity == "Minute" {
// newStart := start[:timeLen-2] + "00"
// newEnd := end[:timeLen-2] + "59"
// return this.Retrieve(newStart, newEnd, "Second")
// }
// if granularity == "Hour" {
// newStart := start[:timeLen-5] + "00:00"
// newEnd := end[:timeLen-5] + "59:59"
// return this.Retrieve(newStart, newEnd, "Second")
// }
// if granularity == "Day" {
// newStart := start[:timeLen-8] + "00:00:00"
// newEnd := end[:timeLen-8] + "23:59:59"
// return this.Retrieve(newStart, newEnd, "Second")
// }
// if granularity == "Month" {
// newStart := start[:timeLen-11] + "01:00:00:00"
// newEnd := end[:timeLen-11] + "31:23:59:59" // 不用纠结,一个月是几天,肯定小于31天
// return this.Retrieve(newStart, newEnd, "Second")
// }
// if granularity == "Year" {
// newStart := start[:timeLen-14] + "01:00:00:00:00"
// newEnd := end[:timeLen-14] + "12:31:23:59:59" // 不用纠结,一个月是几天,肯定小于31天
// return this.Retrieve(newStart, newEnd, "Second")
// }
// return res
// }
// type pair struct {
// id int
// ts string
// }
// type LogSystem struct {
// idsAndTs []pair
// tsWithGran map[string]int
// }
// func Constructor() LogSystem {
// tsWithGran := map[string]int{
// "Year": 5,
// "Month": 8,
// "Day": 11,
// "Hour": 14,
// "Minute": 17,
// "Second": 19,
// }
// return LogSystem{
// idsAndTs: []pair{},
// tsWithGran: tsWithGran,
// }
// }
// func (this *LogSystem) Put(id int, timestamp string) {
// this.idsAndTs = append(this.idsAndTs, pair{id, timestamp})
// }
// func (this *LogSystem) getTm(t string, gran string) string {
// l := this.tsWithGran[gran]
// return t[:l]
// }
// func (this *LogSystem) Retrieve(start string, end string, granularity string) []int {
// s := this.getTm(start, granularity)
// e := this.getTm(end, granularity)
// res := []int{}
// for _, p := range this.idsAndTs {
// ts := this.getTm(p.ts, granularity)
// if ts >= s && ts <= e {
// res = append(res, p.id)
// }
// }
// return res
// }
func main() {
// Explanation
// LogSystem logSystem = new LogSystem();
obj := Constructor()
fmt.Println(obj)
// logSystem.put(1, "2017:01:01:23:59:59");
obj.Put(1,"2017:01:01:23:59:59")
fmt.Println(obj)
// logSystem.put(2, "2017:01:01:22:59:59");
obj.Put(2, "2017:01:01:22:59:59")
fmt.Println(obj)
// logSystem.put(3, "2016:01:01:00:00:00");
obj.Put(3, "2016:01:01:00:00:00")
fmt.Println(obj)
// // return [3,2,1], because you need to return all logs between 2016 and 2017.
// logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year");
fmt.Println(obj.Retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year")) // [3,2,1]
// // return [2,1], because you need to return all logs between Jan. 1, 2016 01:XX:XX and Jan. 1, 2017 23:XX:XX.
// // Log 3 is not returned because Jan. 1, 2016 00:00:00 comes before the start of the range.
// logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour");
fmt.Println(obj.Retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour")) // [2,1]
}