Le migliori 150 intervisteFacile

Archivio valori chiave basato sul tempo

Guida dettagliata e implementazione Python per il problema "Archivio valori chiave basato sul tempo".

Dichiarazione del problema

Facile

Progettare una struttura dati chiave-valore basata sul tempo in grado di memorizzare più valori per la stessa chiave con timestamp diversi e recuperare il valore della chiave a un determinato timestamp.

Implementa la classe TimeMap:

- TimeMap() Inizializza l'oggetto.

- set(key: str, value: str, timestamp: int) Memorizza la chiave key con il valore value al momento specificato timestamp.

- get(key: str, timestamp: int) -> str Restituisce un valore tale che set è stato chiamato in precedenza, con timestamp_prev <= timestamp. Se sono presenti più valori di questo tipo, restituisce il valore associato al più grande timestamp_prev. Se non sono presenti valori, restituisce "".

Vincoli
  • 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

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche della ricerca binaria come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.