상위 150개 인터뷰쉬움

시간 기반 키 값 저장소

'시간 기반 키 값 저장소' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

서로 다른 타임스탬프에서 동일한 키에 대한 여러 값을 저장하고 특정 타임스탬프에서 키 값을 검색할 수 있는 시간 기반 키-값 데이터 구조를 설계합니다.

TimeMap 클래스를 구현합니다.

- TimeMap() 객체를 초기화합니다.

- set(key: str, value: str, timestamp: int) 주어진 시간 timestampvalue 값으로 key 키를 저장합니다.

- get(key: str, timestamp: int) -> str timestamp_prev <= timestamp을 사용하여 이전에 set이 호출된 것과 같은 값을 반환합니다. 이러한 값이 여러 개인 경우 가장 큰 timestamp_prev과 관련된 값을 반환합니다. 값이 없으면 ""을 반환합니다.

제약
  • 1 <= key.length, value.length <= 100
  • key and value consist of lowercase English letters and digits
  • 1 <= timestamp <= 10^7
  • All timestamps of set are strictly increasing for each key
  • At most 2 * 10^5 calls will be made to set and get

Example 1
Input
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output
[None, None, "bar", "bar", None, "bar2", "bar2"]
Explanation

set("foo", "bar", 1): stores bar at time 1. get("foo", 1): returns "bar". get("foo", 3): returns "bar" (latest value at or before time 3). set("foo", "bar2", 4): stores bar2 at time 4. get("foo", 4): returns "bar2". get("foo", 5): returns "bar2".

Need a Hint?
세트나 힙과 같은 이진 검색 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.