LeetCode in Kotlin

2492. Minimum Score of a Path Between Two Cities

Medium

You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that there is a bidirectional road between cities ai and bi with a distance equal to distancei. The cities graph is not necessarily connected.

The score of a path between two cities is defined as the minimum distance of a road in this path.

Return the minimum possible score of a path between cities 1 and n.

Note:

Example 1:

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

Output: 5

Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5. It can be shown that no other path has less score.

Example 2:

Input: n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]

Output: 2

Explanation: The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.

Constraints:

Solution

@Suppress("NAME_SHADOWING")
class Solution {
    fun minScore(n: Int, roads: Array<IntArray>): Int {
        val parent = IntArray(n + 1)
        val rank = IntArray(n + 1)
        for (i in 1..n) {
            parent[i] = i
            rank[i] = Int.MAX_VALUE
        }
        var ans = Int.MAX_VALUE
        for (road in roads) {
            union(parent, rank, road[0], road[1], road[2])
        }
        val u: Int = find(parent, 1)
        val v: Int = find(parent, n)
        if (u == v) {
            ans = rank[u]
        }
        return ans
    }

    private fun find(parent: IntArray, x: Int): Int {
        return if (x == parent[x]) x else find(parent, parent[x]).also { parent[x] = it }
    }

    private fun union(parent: IntArray, rank: IntArray, u: Int, v: Int, w: Int) {
        var u = u
        var v = v
        u = find(parent, u)
        v = find(parent, v)
        if (rank[u] <= rank[v]) {
            parent[v] = u
            rank[u] = Math.min(rank[u], w)
        } else {
            parent[u] = v
            rank[v] = Math.min(rank[v], w)
        }
        return
    }
}