LeetCode in Kotlin

1857. Largest Color Value in a Directed Graph

Hard

There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.

You are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates that there is a directed edge from node aj to node bj.

A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk such that there is a directed edge from xi to xi+1 for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.

Return the largest color value of any valid path in the given graph, or -1 if the graph contains a cycle.

Example 1:

Input: colors = “abaca”, edges = [[0,1],[0,2],[2,3],[3,4]]

Output: 3

Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).

Example 2:

Input: colors = “a”, edges = [[0,0]]

Output: -1

Explanation: There is a cycle from 0 to 0.

Constraints:

Solution

class Solution {
    fun largestPathValue(colors: String, edges: Array<IntArray>): Int {
        val len = colors.length
        val graph = buildGraph(len, edges)
        val frequencies = IntArray(26)
        val calculatedFrequencies = HashMap<Int, IntArray>()
        val status = IntArray(len)
        for (i in 0 until len) {
            if (status[i] != 0) {
                continue
            }
            val localMax = runDFS(graph, i, calculatedFrequencies, status, colors)
            if (localMax!![26] == -1) {
                frequencies.fill(-1)
                break
            } else {
                for (color in 0..25) {
                    frequencies[color] = Math.max(frequencies[color], localMax[color])
                }
            }
        }
        var max = Int.MIN_VALUE
        for (freq in frequencies) {
            max = Math.max(max, freq)
        }
        return max
    }

    private fun runDFS(
        graph: Array<MutableList<Int>?>,
        node: Int,
        calculatedFrequencies: HashMap<Int, IntArray>,
        status: IntArray,
        colors: String
    ): IntArray? {
        if (calculatedFrequencies.containsKey(node)) {
            return calculatedFrequencies[node]
        }
        val frequencies = IntArray(27)
        if (status[node] == 1) {
            frequencies[26] = -1
            return frequencies
        }
        status[node] = 1
        for (neighbour in graph[node]!!) {
            val localMax = runDFS(graph, neighbour, calculatedFrequencies, status, colors)
            if (localMax!![26] == -1) {
                return localMax
            }
            for (i in 0..25) {
                frequencies[i] = Math.max(frequencies[i], localMax[i])
            }
        }
        status[node] = 2
        val color = colors[node].code - 'a'.code
        frequencies[color]++
        calculatedFrequencies[node] = frequencies
        return frequencies
    }

    private fun buildGraph(n: Int, edges: Array<IntArray>): Array<MutableList<Int>?> {
        val graph: Array<MutableList<Int>?> = arrayOfNulls(n)
        for (i in 0 until n) {
            graph[i] = ArrayList()
        }
        for (edge in edges) {
            graph[edge[0]]?.add(edge[1])
        }
        return graph
    }
}