FastPrepChoose a Road to Maximize Average Route Toll Increase

Choose a Road to Maximize Average Route Toll Increase

Harness logoHarness● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

A directed acyclic graph has cities 0 through n-1. Each road is [from,to,toll]. Every route means a directed path from city 0 to city n-1.

Increase exactly one road's toll by one. Return the zero-based input index that maximizes the average toll increase across all routes. Roads on no complete route contribute zero; break ties by smaller input index. Return -1 when no route exists.

Function

bestRoadToIncrease(n: int, roads: int[][]) → int

Examples

Example 1

n = 4roads = [[0,1,5],[0,2,1],[1,3,2],[2,3,9]]return = 0

Every road belongs to one of two routes, so index zero wins the tie.

Example 2

n = 4roads = [[0,1,1],[1,2,1],[1,3,1],[2,3,1]]return = 0

Road 0 appears on both complete routes.

Example 3

n = 3roads = [[0,1,2]]return = -1

No route reaches the destination.

Constraints

  • 2 <= n <= 100000.
  • The graph is a DAG.
  • Path counts fit in a signed 64-bit integer.

More Harness problems

See Harness hiring insights
public int bestRoadToIncrease(int n, int[][] roads) {
    // write your code here
}
n4
roads[[0,1,5],[0,2,1],[1,3,2],[2,3,9]]
expected0
Checking account…