상위 150개 인터뷰쉬움

네트워크 지연 시간

'네트워크 지연 시간' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

1부터 n까지 레이블이 지정된 n개의 노드로 구성된 네트워크가 제공됩니다. 또한 방향이 지정된 간선으로 이동 시간 목록인 시간이 제공됩니다. times[i] = [ui, vi, wi], 여기서 ui는 소스 노드, vi는 대상 노드, wi는 신호가 소스에서 대상으로 이동하는 데 걸리는 시간입니다.

주어진 노드 k에서 신호를 보냅니다. n개 노드 모두가 신호를 수신하는 데 걸리는 최소 시간을 반환합니다. n개의 노드 모두가 신호를 수신하는 것이 불가능하면 -1을 반환합니다.

networkDelayTime(times: List[List[int]], n: int, k: int) -> int 함수를 작성하세요.

제약
  • 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

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?
세트나 힙과 같은 고급 그래프 관련 데이터 구조를 사용해 보세요.
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 리소스

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