LeetCode in Kotlin

1782. Count Pairs Of Nodes

Hard

You are given an undirected graph defined by an integer n, the number of nodes, and a 2D integer array edges, the edges in the graph, where edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi. You are also given an integer array queries.

Let incident(a, b) be defined as the number of edges that are connected to either node a or b.

The answer to the jth query is the number of pairs of nodes (a, b) that satisfy both of the following conditions:

Return an array answers such that answers.length == queries.length and answers[j] is the answer of the jth query.

Note that there can be multiple edges between the same two nodes.

Example 1:

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

Output: [6,5]

Explanation: The calculations for incident(a, b) are shown in the table above. The answers for each of the queries are as follows:

Example 2:

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

Output: [10,10,9,8,6]

Constraints:

Solution

class Solution {
    fun countPairs(n: Int, edges: Array<IntArray>, queries: IntArray): IntArray {
        val edgeCount: MutableMap<Int, Int> = HashMap()
        val degree = IntArray(n)
        for (e in edges) {
            val u = e[0] - 1
            val v = e[1] - 1
            degree[u]++
            degree[v]++
            val eId = Math.min(u, v) * n + Math.max(u, v)
            edgeCount[eId] = edgeCount.getOrDefault(eId, 0) + 1
        }
        val degreeCount: MutableMap<Int, Int> = HashMap()
        var maxDegree = 0
        for (d in degree) {
            degreeCount[d] = degreeCount.getOrDefault(d, 0) + 1
            maxDegree = Math.max(maxDegree, d)
        }
        val count = IntArray(2 * maxDegree + 1)
        for (d1 in degreeCount.entries) {
            for (d2 in degreeCount.entries) {
                count[d1.key + d2.key] += if (d1 === d2) d1.value * (d1.value - 1) else d1.value * d2.value
            }
        }
        for (i in count.indices) {
            count[i] /= 2
        }
        for ((key, value) in edgeCount) {
            val u = key / n
            val v = key % n
            count[degree[u] + degree[v]]--
            count[degree[u] + degree[v] - value]++
        }
        for (i in count.size - 2 downTo 0) {
            count[i] += count[i + 1]
        }
        val res = IntArray(queries.size)
        for (q in queries.indices) {
            res[q] = if (queries[q] + 1 >= count.size) 0 else count[queries[q] + 1]
        }
        return res
    }
}