파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 10. 6. 23:45

https://leetcode.com/problems/time-based-key-value-store/

 

Time Based Key-Value Store - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

주어진 조건에 따라 클래스 만드는거라 약간 모델링 하는거랑 비슷.

class TimeMap:

    def __init__(self):
        self.m = {}

    def set(self, key: str, value: str, timestamp: int) -> None:
        if not key in self.m:
            self.m[key] = {}
        
        self.m[key][timestamp] = value
        

    def get(self, key: str, timestamp: int) -> str:
        if not key in self.m:
            return ''
        
        for curr_time in reversed(range(1, timestamp+1)):
            if curr_time in self.m[key]:
                return self.m[key][curr_time]

        return ''
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.10.08
Today's Challenge  (0) 2022.10.07
Today's Challenge  (0) 2022.10.05
Today's Challenge  (0) 2022.10.04
Today's Challenge  (1) 2022.10.03
Comments