Design Log Storage System — LeetCode 635 Python Solution
- Problem
- #635
- Pattern
- Hash Map
- Reading time
- 5 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- ["LogSystem", "put", "put", "put", "retrieve", "retrieve"]
- Output
- [null, null, null, null, [3, 2, 1], [2, 1]]
- Explanation
- LogSystem logSystem = new LogSystem();
Python solution
class LogSystem:
def __init__(self):
self.logs = []
self.d = {
"Year": 4,
"Month": 7,
"Day": 10,
"Hour": 13,
"Minute": 16,
"Second": 19,
}
def put(self, id: int, timestamp: str) -> None:
self.logs.append((id, timestamp))
def retrieve(self, start: str, end: str, granularity: str) -> List[int]:
i = self.d[granularity]
return [id for id, ts in self.logs if start[:i] <= ts[:i] <= end[:i]]
# Your LogSystem object will be instantiated and called as such:
# obj = LogSystem()
# obj.put(id,timestamp)
# param_2 = obj.retrieve(start,end,granularity)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 635. Design Log Storage System is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 635. Design Log Storage System?
- LeetCode 635. Design Log Storage System is rated Medium on LeetCode.
- What is the time complexity of LeetCode 635. Design Log Storage System?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 635. Design Log Storage System?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 635. Design Log Storage System cover?
- LeetCode 635. Design Log Storage System is tagged Design, Hash Table, String and Ordered Set on LeetCode.
- Is LeetCode 635. Design Log Storage System a premium problem?
- Yes. LeetCode 635. Design Log Storage System is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.