LeetCode in Kotlin

924. Minimize Malware Spread

Hard

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove exactly one node from initial.

Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]

Output: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]

Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]

Output: 1

Constraints:

Solution

class Solution {
    private lateinit var size: IntArray
    private lateinit var par: IntArray

    fun minMalwareSpread(graph: Array<IntArray>, initial: IntArray): Int {
        size = IntArray(graph.size)
        par = IntArray(graph.size)
        for (i in graph.indices) {
            size[i] = 1
            par[i] = i
        }
        // create groups
        for (i in graph.indices) {
            for (j in graph[0].indices) {
                if (graph[i][j] == 1) {
                    val p1 = find(i)
                    val p2 = find(j)
                    merge(p1, p2)
                }
            }
        }
        // no of infected in group
        val infected = IntArray(graph.size)
        for (e in initial) {
            val p = find(e)
            infected[p]++
        }
        var currSize = -1
        var ans = -1
        for (e in initial) {
            val p = find(e)
            if (infected[p] == 1 && size[p] >= currSize) {
                ans = if (size[p] > currSize) {
                    e
                } else {
                    ans.coerceAtMost(e)
                }
                currSize = size[p]
            }
        }
        // all groups have more than 1 infected node then return min value from initial
        if (ans == -1) {
            ans = initial[0]
            for (j in initial) {
                ans = ans.coerceAtMost(j)
            }
        }
        return ans
    }

    private fun merge(p1: Int, p2: Int) {
        if (p1 != p2) {
            if (size[p1] > size[p2]) {
                par[p2] = p1
                size[p1] += size[p2]
            } else {
                par[p1] = p2
                size[p2] += size[p1]
            }
        }
    }

    private fun find(u: Int): Int {
        if (par[u] == u) {
            return u
        }
        par[u] = find(par[u])
        return par[u]
    }
}