Problem · Graph

Dynamic Broadcast Times

Learn this problem
HardNetflix logoNetflixFULLTIMEPHONE SCREEN

Problem statement

A network contains n cities numbered from 0 to n - 1. Each directed connection has a positive ping time. The row edges[i] = [from, to, weight] defines edge i; edge indices remain fixed throughout the operation sequence.

Process the operations in order:

  • [0, edgeIndex, newWeight] replaces the weight of the existing edge at edgeIndex with newWeight.
  • [1, source] asks for the fastest broadcast time from source to every city using the current weights.

For every query, append an array of length n in city-index order. The distance to source is 0, and the distance to an unreachable city is -1. Return the query-result arrays in operation order.

Function

processBroadcastOperations(n: int, edges: long[][], operations: long[][]) → long[][]

Examples

Example 1

n = 4edges = [[0,1,5],[0,2,20],[1,2,4],[2,3,3]]operations = [[1,0],[0,1,6],[1,0],[0,0,15],[1,0]]return = [[0,5,9,12],[0,5,6,9],[0,15,6,9]]

The first query reaches city 2 through city 1. Lowering edge 1 makes the direct route faster. Raising edge 0 later changes city 1 without affecting the direct route to city 2.

Example 2

n = 3edges = [[0,1,2]]operations = [[1,0],[1,2],[0,0,7],[1,0]]return = [[0,2,-1],[-1,-1,0],[0,7,-1]]

City 2 cannot reach either other city. Updating the only edge changes the distance from city 0 to city 1, while city 2 remains unreachable.

Example 3

n = 5edges = [[0,1,10],[0,2,2],[2,1,3],[1,3,2],[2,3,20],[3,4,1]]operations = [[1,0],[0,4,1],[1,0],[1,2]]return = [[0,5,2,7,8],[0,5,2,3,4],[-1,3,0,1,2]]

After edge 4 decreases to weight 1, the route from city 0 through city 2 reaches cities 3 and 4 sooner. The final query uses city 2 as its source.

Constraints

  • 1 <= n <= 500.
  • 0 <= edges.length <= 5000.
  • Every edge is [from, to, weight], where both endpoints are valid city indices and 1 <= weight <= 10^9.
  • 1 <= operations.length <= 5000, and at least one operation is a query.
  • Every update is [0, edgeIndex, newWeight], where edgeIndex names an existing edge and 1 <= newWeight <= 10^9.
  • Every query is [1, source], where 0 <= source < n.
  • Every shortest-path distance fits a signed 64-bit integer.

More Netflix problems

drafts saved locally
public long[][] processBroadcastOperations(int n, long[][] edges, long[][] operations) {
    // Write your code here.
}
n4
edges[[0,1,5],[0,2,20],[1,2,4],[2,3,3]]
operations[[1,0],[0,1,6],[1,0],[0,0,15],[1,0]]
expected[[0,5,9,12],[0,5,6,9],[0,15,6,9]]
checking account