같은 message가 10초 안에 다시 Print 되지 않도록 확인하는 함수를 만들면 된다. 파이썬의 해시맵을 사용해 해당 message의 timestamp를 확인하고 갱신해 True/False를 반환한다.
class Logger:
def __init__(self):
self.d = defaultdict(int)
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
if message not in self.d:
self.d[message] = timestamp
return True
else:
if timestamp - self.d[message] < 10:
return False
else:
self.d[message] = timestamp
return True
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
[LeetCode] 582. Kill Process (0) | 2023.05.06 |
---|---|
[LeetCode] 604. Design Compressed String Iterator (0) | 2023.05.05 |
[LeetCode] 360. Sort Transformed Array (0) | 2023.04.01 |
[LeetCode] 298. Binary Tree Longest Consecutive Sequence (2) | 2023.03.03 |
[LeetCode] 296. Best Meeting Point (0) | 2023.03.02 |