Design Log Storage System Problem
Description
LeetCode Problem 635.
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 theLogSystemobject.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 fromstarttoendinclusive.startandendall have the same format astimestamp, andgranularitymeans how precise the range should be (i.e. to the exactDay,Minute, etc.). For example,start = "2017:01:01:23:59:59",end = "2017:01:02:23:59:59", andgranularity = "Day"means that we need to find the logs within the inclusive range from Jan. 1st 2017 to Jan. 2nd 2017, and theHour,Minute, andSecondfor 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 <= 5002000 <= Year <= 20171 <= Month <= 121 <= Day <= 310 <= Hour <= 230 <= Minute, Second <= 59granularityis one of the values["Year", "Month", "Day", "Hour", "Minute", "Second"].- At most
500calls will be made toputandretrieve.
Solution
Approach: Compare Timestamp Prefixes as Strings
The timestamp format YYYY:MM:DD:HH:MM:SS is fixed-width and every field is zero-padded. So comparing two timestamps as plain strings, character by character, gives the same order as comparing the times. For example "2016:12:31..." < "2017:01:01...".
Granularity also has a simple string meaning. Comparing "to the exact Day" means looking only at the year, month and day, and those are exactly the first 10 characters. Each granularity corresponds to a prefix length:
Year 4, Month 7, Day 10, Hour 13, Minute 16, Second 19.
put(id, timestamp): append the pair to a list.retrieve(start, end, granularity): takek, the prefix length ofgranularity, and return the IDs of all logs whosetimestamp[:k]is betweenstart[:k]andend[:k], both inclusive.
Cutting start and end to the same prefix is what makes the coarser granularities work. In the example, with Hour, start becomes "2016:01:01:01", so log 3 at hour 00 is left out, while with Year both bounds shrink to "2016" and "2017" and every log is in range. Do not convert the bounds to "start of day" and "end of day" by hand; cutting the prefix already does it.
Complexity: put is O(1). retrieve scans all n stored logs, O(n) each comparing a fixed-length prefix. O(n) space.
class LogSystem:
# How many characters of "YYYY:MM:DD:HH:MM:SS" each granularity keeps.
PREFIX = {"Year": 4, "Month": 7, "Day": 10, "Hour": 13, "Minute": 16, "Second": 19}
def __init__(self):
self.logs = []
def put(self, id: int, timestamp: str) -> None:
self.logs.append((id, timestamp))
def retrieve(self, start: str, end: str, granularity: str) -> List[int]:
# Zero-padded fields: comparing the prefixes as strings compares the times.
k = self.PREFIX[granularity]
return [id for id, ts in self.logs if start[:k] <= ts[:k] <= end[:k]]