Le migliori 150 intervisteFacile

Tempo di ritardo della rete

Guida dettagliata e implementazione Python per il problema "Tempo di ritardo della rete".

Dichiarazione del problema

Facile

Ti viene data una rete di n nodi, etichettati da 1 a n. Vengono inoltre forniti i tempi, un elenco di tempi di viaggio come bordi diretti times[i] = [ui, vi, wi], dove ui è il nodo sorgente, vi è il nodo di destinazione e wi è il tempo impiegato da un segnale per viaggiare dalla sorgente alla destinazione.

Invieremo un segnale da un dato nodo k. Restituisce il tempo minimo impiegato da tutti gli n nodi per ricevere il segnale. Se è impossibile per tutti gli n nodi ricevere il segnale, restituisce -1.

Scrivi una funzione networkDelayTime(times: List[List[int]], n: int, k: int) -> int.

Vincoli
  • 1 <= k <= n <= 100
  • 1 <= len(times) <= 6000
  • times[i].length == 3
  • 1 <= ui, vi <= n
  • ui != vi
  • 0 <= wi <= 100
  • All the pairs (ui, vi) are unique

Esempi

Example 1
Input
times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output
2
Explanation

The signal starts at node 2. It reaches 1 and 3 in 1 unit of time, and 4 in 2 units of time.

Example 2
Input
times = [[1,2,1]], n = 2, k = 1
Output
1
Explanation

Signal reaches node 2 from node 1 in 1 unit of time.

Example 3
Input
times = [[1,2,1]], n = 2, k = 2
Output
-1
Explanation

Signal starts at node 2, but there is no path from node 2 to node 1. So node 1 never receives it.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Advanced Graphs 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.