Skip to content

Commit 9d0f465

Browse files
committed
Runtime: 2578 ms (Top 92.45%) | Memory: 69.3 MB (Top 19.78%)
1 parent a067887 commit 9d0f465

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
1+
# Runtime: 2578 ms (Top 92.45%) | Memory: 69.3 MB (Top 19.78%)
12
class Solution:
23
def networkBecomesIdle(self, edges: List[List[int]], patience: List[int]) -> int:
3-
4+
45
#Build Adjency List
56
adjList = defaultdict(list)
6-
7+
78
for source, target in edges:
89
adjList[source].append(target)
910
adjList[target].append(source)
10-
11-
11+
1212
#BFS to get the shortest route from node to master.
1313
shortest = {}
1414
queue = deque([(0,0)])
1515
seen = set()
1616
while queue:
1717
currPos, currDist = queue.popleft()
18-
18+
1919
if currPos in seen:
2020
continue
2121
seen.add(currPos)
2222
shortest[currPos] = currDist
23-
23+
2424
for nei in adjList[currPos]:
2525
queue.append((nei, currDist+1))
2626

27-
2827
#Calculate answer using shortest paths.
2928
ans = 0
3029
for index in range(1,len(patience)):
3130
resendInterval = patience[index]
32-
31+
3332
#The server will stop sending requests after it's been sent to the master node and back.
3433
shutOffTime = (shortest[index] * 2)
35-
34+
3635
# shutOffTime-1 == Last second the server can send a re-request.
3736
lastSecond = shutOffTime-1
38-
37+
3938
#Calculate the last time a packet is actually resent.
4039
lastResentTime = (lastSecond//resendInterval)*resendInterval
41-
40+
4241
# At the last resent time, the packet still must go through 2 more cycles to the master node and back.
4342
lastPacketTime = lastResentTime + shutOffTime
44-
43+
4544
ans = max(lastPacketTime, ans)
46-
45+
4746
#Add +1, the current answer is the last time the packet is recieved by the target server (still active).
4847
#We must return the first second the network is idle, therefore + 1
49-
return ans + 1
48+
return ans + 1

0 commit comments

Comments
 (0)