LeetCode in Kotlin

2039. The Time When the Network Becomes Idle

Medium

There is a network of n servers, labeled from 0 to n - 1. You are given a 2D integer array edges, where edges[i] = [ui, vi] indicates there is a message channel between servers ui and vi, and they can pass any number of messages to each other directly in one second. You are also given a 0-indexed integer array patience of length n.

All servers are connected, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.

The server labeled 0 is the master server. The rest are data servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers optimally, so every message takes the least amount of time to arrive at the master server. The master server will process all newly arrived messages instantly and send a reply to the originating server via the reversed path the message had gone through.

At the beginning of second 0, each data server sends its message to be processed. Starting from second 1, at the beginning of every second, each data server will check if it has received a reply to the message it sent (including any newly arrived replies) from the master server:

The network becomes idle when there are no messages passing between servers or arriving at servers.

Return the earliest second starting from which the network becomes idle.

Example 1:

example 1

Input: edges = [[0,1],[1,2]], patience = [0,2,1]

Output: 8

Explanation:

At (the beginning of) second 0,

At second 1,

At second 2,

At second 4,

At second 7, reply 2D arrives at server 2.

Starting from the beginning of the second 8, there are no messages passing between servers or arriving at servers.

This is the time when the network becomes idle.

Example 2:

example 2

Input: edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]

Output: 3

Explanation: Data servers 1 and 2 receive a reply back at the beginning of second 2.

From the beginning of the second 3, the network becomes idle.

Constraints:

Solution

import java.util.PriorityQueue

class Solution {
    fun networkBecomesIdle(edges: Array<IntArray>, pat: IntArray): Int {
        val n = pat.size
        val adj = ArrayList<ArrayList<Int>>()
        for (i in 0 until n) {
            adj.add(ArrayList())
        }
        for (arr in edges) {
            adj[arr[0]].add(arr[1])
            adj[arr[1]].add(arr[0])
        }
        val distance = IntArray(n)
        distance.fill(99999)
        distance[0] = 0
        val pq = PriorityQueue { a1: IntArray, a2: IntArray ->
            Integer.compare(
                a1[1],
                a2[1],
            )
        }
        pq.add(intArrayOf(0, 0))
        while (pq.isNotEmpty()) {
            val a = pq.poll()
            val node = a[0]
            for (nn in adj[node]) {
                if (distance[node] + 1 < distance[nn]) {
                    distance[nn] = 1 + distance[node]
                    pq.add(intArrayOf(nn, distance[nn]))
                }
            }
        }
        var max = 0
        for (i in 1 until n) {
            val num1 = 2 * distance[i]
            var num2 = num1 / pat[i]
            if (num1 % pat[i] != 0) {
                num2++
            }
            num2--
            num2 *= pat[i]
            max = Math.max(max, num2 + num1)
        }
        return max + 1
    }
}