Skip to the code
cf.
+25 XP

#459 When Does the Last Edge Server Update?

medium

A CDN pushes a config update from an origin server to n edge servers numbered 0 to n - 1. Each links[i] = [from, to, ms] means server from can forward the update to server to, and it takes ms milliseconds to arrive. Every server forwards the update as soon as it receives it.

Write lastUpdateTime(n, links, origin) that returns how many milliseconds pass until every server has the update, or -1 if some server never gets it.

Example 1

Input: n = 4, links = [[0,1,1],[0,2,4],[1,2,2],[2,3,1]], origin = 0
Output: 4

Server 2 is fastest through 1 (1 + 2 = 3 ms), so server 3 gets it at 4 ms, the last one.

Example 2

Input: n = 2, links = [[0,1,5]], origin = 1
Output: -1

The only link goes from server 0 to server 1, so the update can't travel from server 1 back to server 0.

Constraints

  • 1 <= n <= 1000
  • 0 <= links.length <= 10000
  • 0 <= ms <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).