-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0635-design-log-storage-system.js
More file actions
51 lines (46 loc) · 1.23 KB
/
0635-design-log-storage-system.js
File metadata and controls
51 lines (46 loc) · 1.23 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
/**
* Design Log Storage System
* Time Complexity: O(N * L + N log N)
* Space Complexity: O(N * L)
*/
var LogSystem = function () {
this.entries = [];
this.granuleMap = {
Year: 4,
Month: 7,
Day: 10,
Hour: 13,
Minute: 16,
Second: 19,
};
};
LogSystem.prototype.put = function (logIdToStore, timeString) {
this.entries.push([timeString, logIdToStore]);
};
LogSystem.prototype.retrieve = function (
queryStartTime,
queryEndTime,
selectedGranularity,
) {
const compareLength = this.granuleMap[selectedGranularity];
const computedStartString = queryStartTime
.slice(0, compareLength)
.padEnd(19, "0");
const computedEndString = queryEndTime
.slice(0, compareLength)
.padEnd(19, "9");
const filteredLogPairs = this.entries.filter(
([logTimeValue, logIdentifierValue]) => {
const parsedLogTimeValue = logTimeValue
.slice(0, compareLength)
.padEnd(19, "0");
return (
parsedLogTimeValue >= computedStartString &&
parsedLogTimeValue <= computedEndString
);
},
);
const extractedIds = filteredLogPairs.map((itemPair) => itemPair[1]);
const sortedResultIds = extractedIds.sort((valA, valB) => valA - valB);
return sortedResultIds;
};