Problem · Graph

Minimum Score of a Path Between Cities

Learn this problem
MediumVisaOA

Problem statement

You are given n cities numbered from 1 through n and an array roads. Each roads[i] = [a, b, distance] describes a bidirectional road between cities a and b.

The score of a path is the minimum road distance used anywhere along that path. Cities and roads may be visited more than once.

Return the minimum possible score of a path from city 1 to city n. At least one such path exists.

Function

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

Examples

Example 1

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

The path 1 -> 2 -> 4 has score min(9, 5) = 5, which is optimal.

Example 2

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

Because revisits are allowed, a walk can use the road of length 2 before continuing to city 4.

More Visa problems

drafts saved locally
public int minScore(int n, int[][] roads) {
  // write your code here
}
n4
roads[[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
expected5
checking account