Skip to content

Commit 8ba76ee

Browse files
committed
743-network-delay-time.md Added Python solution.
1 parent eca482d commit 8ba76ee

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

en/1-1000/743-network-delay-time.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 743. Network Delay Time - Best Practices of LeetCode Solutions
2+
LeetCode link: [743. Network Delay Time](https://leetcode.com/problems/network-delay-time), difficulty: **Medium**.
3+
4+
## LeetCode description of "743. Network Delay Time"
5+
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target.
6+
7+
We will send a signal from a given node `k`. Return _the **minimum** time it takes for all the n nodes to receive the signal_. If it is impossible for all the `n` nodes to receive the signal, return `-1`.
8+
9+
### [Example 1]
10+
![](../../images/examples/743_1.png)
11+
12+
**Input**: `times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2`
13+
14+
**Output**: `2`
15+
16+
### [Example 2]
17+
**Input**: `times = [[1,2,1]], n = 2, k = 1`
18+
19+
**Output**: `1`
20+
21+
### [Example 3]
22+
**Input**: `times = [[1,2,1]], n = 2, k = 2`
23+
24+
**Output**: `-1`
25+
26+
### [Constraints]
27+
- `1 <= k <= n <= 100`
28+
- `1 <= times.length <= 6000`
29+
- `times[i].length == 3`
30+
- `1 <= ui, vi <= n`
31+
- `ui != vi`
32+
- `0 <= wi <= 100`
33+
- All the pairs `(ui, vi)` are **unique**. (i.e., no multiple edges.)
34+
35+
### [Hints]
36+
<details>
37+
<summary>Hint 1</summary>
38+
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
39+
</details>
40+
41+
## Intuition
42+
43+
## Complexity
44+
* Time: `O()`.
45+
* Space: `O()`.
46+
47+
## Python
48+
```python
49+
class Solution:
50+
def networkDelayTime(self, items: List[List[int]], n: int, start: int) -> int:
51+
min_distances = [float('inf')] * (n + 1)
52+
min_distances[start] = 0
53+
54+
node_to_pairs = defaultdict(set)
55+
56+
for source, target, distance in items:
57+
node_to_pairs[source].add((target, distance))
58+
59+
for _ in range(n - 1):
60+
for node, min_distance in enumerate(min_distances):
61+
if min_distance == float('inf'):
62+
continue
63+
64+
for target_node, distance in node_to_pairs[node]:
65+
distance_ = distance + min_distance
66+
67+
if distance_ < min_distances[target_node]:
68+
min_distances[target_node] = distance_
69+
70+
result = max(min_distances[1:])
71+
72+
if result == float('inf'):
73+
return -1
74+
75+
return result
76+
```
77+
78+
## JavaScript
79+
```javascript
80+
// Welcome to create a PR to complete the code of this language, thanks!
81+
```
82+
83+
## Java
84+
```java
85+
// Welcome to create a PR to complete the code of this language, thanks!
86+
```
87+
88+
## C++
89+
```cpp
90+
// Welcome to create a PR to complete the code of this language, thanks!
91+
```
92+
93+
## C#
94+
```c#
95+
// Welcome to create a PR to complete the code of this language, thanks!
96+
```
97+
98+
## Go
99+
```go
100+
// Welcome to create a PR to complete the code of this language, thanks!
101+
```
102+
103+
## Ruby
104+
```ruby
105+
# Welcome to create a PR to complete the code of this language, thanks!
106+
```
107+
108+
## C, Kotlin, Swift, Rust or other languages
109+
```
110+
// Welcome to create a PR to complete the code of this language, thanks!
111+
```

0 commit comments

Comments
 (0)