LeetCode in Kotlin

1579. Remove Max Number of Edges to Keep Graph Fully Traversable

Hard

Alice and Bob have an undirected graph of n nodes and 3 types of edges:

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if it’s impossible for the graph to be fully traversed by Alice and Bob.

Example 1:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]

Output: 2

Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.

Example 2:

Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]

Output: 0

Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.

Example 3:

Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]

Output: -1

Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it’s impossible to make the graph fully traversable.

Constraints:

Solution

class Solution {
    fun maxNumEdgesToRemove(n: Int, edges: Array<IntArray>): Int {
        edges.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] }
        val alice = IntArray(n + 1)
        val rankAlice = IntArray(n + 1)
        val bob = IntArray(n + 1)
        val rankBob = IntArray(n + 1)
        for (i in 1..n) {
            alice[i] = i
            bob[i] = i
        }
        var countAlice = n
        var countBob = n
        var remove = 0
        for (edge in edges) {
            val type = edge[0]
            val u = edge[1]
            val v = edge[2]
            if (type == 1) {
                val a = union(u, v, alice, rankAlice)
                if (a) {
                    countAlice--
                } else {
                    remove++
                }
            } else if (type == 2) {
                val b = union(u, v, bob, rankBob)
                if (b) {
                    countBob--
                } else {
                    remove++
                }
            } else {
                val b = union(u, v, bob, rankBob)
                val a = union(u, v, alice, rankAlice)
                if (!a && !b) {
                    remove++
                }
                if (a) {
                    countAlice--
                }
                if (b) {
                    countBob--
                }
            }
        }
        return if (countAlice != 1 || countBob != 1) {
            -1
        } else remove
    }

    fun union(x: Int, y: Int, arr: IntArray, rank: IntArray): Boolean {
        val p1 = find(arr[x], arr)
        val p2 = find(arr[y], arr)
        if (p1 != p2) {
            if (rank[p1] > rank[p2]) {
                arr[p2] = p1
            } else if (rank[p1] < rank[p2]) {
                arr[p1] = p2
            } else {
                arr[p1] = p2
                rank[p2]++
            }
            return true
        }
        return false
    }

    fun find(x: Int, arr: IntArray): Int {
        if (arr[x] == x) {
            return x
        }
        val temp = find(arr[x], arr)
        arr[x] = temp
        return temp
    }
}