Back to Month
MEDIUM 04 Jul 2026

2492. Minimum Score of a Path Between Two Cities

</> Solution

import java.util.*;

class Solution {
    public int minScore(int n, int[][] roads) {
        List<int[]>[] graph = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }

        for (int[] road : roads) {
            graph[road[0]].add(new int[]{road[1], road[2]});
            graph[road[1]].add(new int[]{road[0], road[2]});
        }

        boolean[] vis = new boolean[n + 1];
        Queue<Integer> q = new LinkedList<>();
        q.offer(1);
        vis[1] = true;

        int ans = Integer.MAX_VALUE;

        while (!q.isEmpty()) {
            int u = q.poll();

            for (int[] edge : graph[u]) {
                int v = edge[0];
                int d = edge[1];

                ans = Math.min(ans, d);

                if (!vis[v]) {
                    vis[v] = true;
                    q.offer(v);
                }
            }
        }

        return ans;
    }
}

TIME COMPLEXITY

O(V + E)

SPACE COMPLEXITY

O(V + E)

TOPICS

BFS Graph