|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +You are given several logs that each log contains a unique id and timestamp. |
| 4 | +Timestamp is a string that has the following format: |
| 5 | +Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59. All domains |
| 6 | +are zero-padded decimal numbers. |
| 7 | +
|
| 8 | +Design a log storage system to implement the following functions: |
| 9 | +
|
| 10 | +void Put(int id, string timestamp): Given a log's unique id and timestamp, store |
| 11 | +the log in your storage system. |
| 12 | +
|
| 13 | +
|
| 14 | +int[] Retrieve(String start, String end, String granularity): Return the id of |
| 15 | +logs whose timestamps are within the range from start to end. Start and end all |
| 16 | +have the same format as timestamp. However, granularity means the time level for |
| 17 | +consideration. For example, start = "2017:01:01:23:59:59", end = |
| 18 | +"2017:01:02:23:59:59", granularity = "Day", it means that we need to find the |
| 19 | +logs within the range from Jan. 1st 2017 to Jan. 2nd 2017. |
| 20 | +
|
| 21 | +Example 1: |
| 22 | +put(1, "2017:01:01:23:59:59"); |
| 23 | +put(2, "2017:01:01:22:59:59"); |
| 24 | +put(3, "2016:01:01:00:00:00"); |
| 25 | +retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"); // return [1,2,3], |
| 26 | +because you need to return all logs within 2016 and 2017. |
| 27 | +retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"); // return [1,2], |
| 28 | +because you need to return all logs start from 2016:01:01:01 to 2017:01:01:23, |
| 29 | +where log 3 is left outside the range. |
| 30 | +
|
| 31 | +Note: |
| 32 | +There will be at most 300 operations of Put or Retrieve. |
| 33 | +Year ranges from [2000,2017]. Hour ranges from [00,23]. |
| 34 | +Output for Retrieve has no order required. |
| 35 | +""" |
| 36 | +import bisect |
| 37 | + |
| 38 | + |
| 39 | +class LogSystem: |
| 40 | + def __init__(self): |
| 41 | + """ |
| 42 | + BST - TreeMap (java) |
| 43 | + binary search using time stamp |
| 44 | + """ |
| 45 | + self.lst = [] |
| 46 | + |
| 47 | + def put(self, id: int, timestamp: str) -> None: |
| 48 | + bisect.insort(self.lst, (timestamp, id)) |
| 49 | + |
| 50 | + def retrieve(self, s: str, e: str, gra: str) -> List[int]: |
| 51 | + """ |
| 52 | + Use timestamp comparison |
| 53 | + Can convert the timestamp to number. |
| 54 | + """ |
| 55 | + lo = "0001:01:01:00:00:00" |
| 56 | + hi = "9999:12:31:23:59:59" |
| 57 | + pre = { |
| 58 | + "Year": 4, |
| 59 | + "Month": 7, |
| 60 | + "Day": 10, |
| 61 | + "Hour": 13, |
| 62 | + "Minute": 16, |
| 63 | + "Second": 19, |
| 64 | + }[gra] |
| 65 | + |
| 66 | + s = s[:pre] + lo[pre:] |
| 67 | + e = e[:pre] + hi[pre:] |
| 68 | + i = bisect.bisect_left(self.lst, (s, 0)) |
| 69 | + j = bisect.bisect_right(self.lst, (e, float("inf"))) |
| 70 | + return [id for _, id in self.lst[i:j]] |
| 71 | + |
| 72 | + |
| 73 | +# Your LogSystem object will be instantiated and called as such: |
| 74 | +# obj = LogSystem() |
| 75 | +# obj.put(id,timestamp) |
| 76 | +# param_2 = obj.retrieve(s,e,gra) |
0 commit comments