Minimize a Route’s Smallest Road Distance

Quick Overview

Minimize the smallest road distance used on a route between two cities, allowing repeated roads and cities.

Minimize a Route’s Smallest Road Distance

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Cities are numbered 1 through `n`, and each road is an undirected edge with a positive distance. A route from city 1 to city `n` may repeat roads and cities, including either endpoint. Its score is the smallest road distance used anywhere along that route. Return the minimum possible score. At least one route from 1 to `n` exists, though other cities may be disconnected. ```cpp class Solution { public: int minScore(int n, vector<vector<int>>& roads); }; ``` ### Examples **Example 1:** ```text Input: n = 3, roads = [[1,2,8],[2,3,7],[1,3,9]] Output: 7 Explanation: Route 1-2-3 has score 7, the smallest road weight in that component. ``` **Example 2:** ```text Input: n = 4, roads = [[1,4,10],[1,2,2]] Output: 2 Explanation: Detour 1-2-1-4 includes the weight2 road; isolated city3 is irrelevant. ``` ### Constraints - `2 <= n <= 100000`; `1 <= roads.length <= 100000`. - Each road has distinct endpoints in[1, n]and distance in[1,10000]. - No edge is repeated. - Cities 1 and n are connected; other cities need not be. ### Clarifying Questions - Is the score a sum of distances? No; it is the smallest distance of any road used. - Can a route revisit a city or road? Yes. - Must every city be connected? No; only cities 1 and n are guaranteed connected. - Is a route required in the answer? No; return only its minimum possible score. ```hint Check the boundary Compare a route’s sum, maximum road distance and minimum road distance before choosing an objective. ```

Overview: Minimize the smallest road distance used on a route between two cities, allowing repeated roads and cities.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Cities are numbered 1 through n, and each road is an undirected edge with a positive distance. A route from city 1 to city n may repeat roads and cities, including either endpoint. Its score is the smallest road distance used anywhere along that route. Return the minimum possible score. At least one route from 1 to n exists, though other cities may be disconnected.

class Solution {
public:
    int minScore(int n, vector<vector<int>>& roads);
};

Examples

Example 1:

Input: n = 3, roads = [[1,2,8],[2,3,7],[1,3,9]]
Output: 7
Explanation: Route 1-2-3 has score 7, the smallest road weight in that component.

Example 2:

Input: n = 4, roads = [[1,4,10],[1,2,2]]
Output: 2
Explanation: Detour 1-2-1-4 includes the weight2 road; isolated city3 is irrelevant.

Constraints

  • 2 <= n <= 100000 ; 1 <= roads.length <= 100000 .
  • Each road has distinct endpoints in[1, n]and distance in[1,10000].
  • No edge is repeated.
  • Cities 1 and n are connected; other cities need not be.

Clarifying Questions Guidance

  • Is the score a sum of distances? No; it is the smallest distance of any road used.
  • Can a route revisit a city or road? Yes.
  • Must every city be connected? No; only cities 1 and n are guaranteed connected.
  • Is a route required in the answer? No; return only its minimum possible score.