LeetCode in Kotlin

2497. Maximum Star Sum of a Graph

Medium

There is an undirected graph consisting of n nodes numbered from 0 to n - 1. You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node.

You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

A star graph is a subgraph of the given graph having a center node containing 0 or more neighbors. In other words, it is a subset of edges of the given graph such that there exists a common node for all edges.

The image below shows star graphs with 3 and 4 neighbors respectively, centered at the blue node.

The star sum is the sum of the values of all the nodes present in the star graph.

Given an integer k, return the maximum star sum of a star graph containing at most k edges.

Example 1:

Input: vals = [1,2,3,4,10,-10,-20], edges = [[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], k = 2

Output: 16

Explanation: The above diagram represents the input graph.

The star graph with the maximum star sum is denoted by blue.

It is centered at 3 and includes its neighbors 1 and 4. It can be shown it is not possible to get a star graph with a sum greater than 16.

Example 2:

Input: vals = [-5], edges = [], k = 0

Output: -5

Explanation: There is only one possible star graph, which is node 0 itself. Hence, we return -5.

Constraints:

Solution

import java.util.PriorityQueue

class Solution {
    private lateinit var graphNodeIdToNodeValues: Array<PriorityQueue<Int>?>

    fun maxStarSum(nodeValues: IntArray, edges: Array<IntArray>, maxNumberOfEdges: Int): Int {
        val totalNodes = nodeValues.size
        graphNodeIdToNodeValues = arrayOfNulls(totalNodes)
        for (i in 0 until totalNodes) {
            graphNodeIdToNodeValues[i] = PriorityQueue()
        }
        for (edge in edges) {
            addEdgeEndingWithValueOfNode(nodeValues, edge[0], edge[1], maxNumberOfEdges)
            addEdgeEndingWithValueOfNode(nodeValues, edge[1], edge[0], maxNumberOfEdges)
        }
        return calculateMaxStarSum(nodeValues, totalNodes)
    }

    private fun addEdgeEndingWithValueOfNode(
        nodeValues: IntArray,
        fromNode: Int,
        toNode: Int,
        maxNumberOfEdges: Int,
    ) {
        if (nodeValues[toNode] > 0 && graphNodeIdToNodeValues[fromNode]!!.size < maxNumberOfEdges) {
            graphNodeIdToNodeValues[fromNode]!!.add(nodeValues[toNode])
        } else if (graphNodeIdToNodeValues[fromNode]!!.isNotEmpty() &&
            graphNodeIdToNodeValues[fromNode]!!.peek() < nodeValues[toNode]
        ) {
            graphNodeIdToNodeValues[fromNode]!!.poll()
            graphNodeIdToNodeValues[fromNode]!!.add(nodeValues[toNode])
        }
    }

    private fun calculateMaxStarSum(nodeValues: IntArray, totalNodes: Int): Int {
        var maxStarSum = Int.MIN_VALUE
        for (i in 0 until totalNodes) {
            var sum = nodeValues[i]
            for (value in graphNodeIdToNodeValues[i]!!) {
                sum += value
            }
            maxStarSum = Math.max(maxStarSum, sum)
        }
        return maxStarSum
    }
}