forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1857-largest-color-value-in-a-directed-graph.kt
45 lines (37 loc) · 1.32 KB
/
1857-largest-color-value-in-a-directed-graph.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
fun largestPathValue(colors: String, edges: Array<IntArray>): Int {
val adj = HashMap<Int, ArrayList<Int>>()
for ((from, to) in edges)
adj[from] = adj.getOrDefault(from, ArrayList<Int>()).apply{this.add(to)}
val visited = HashSet<Int>()
val path = HashSet<Int>()
val count = Array(colors.length){IntArray(26)}
fun dfs(node: Int): Int {
if (node in path)
return Integer.MAX_VALUE
if (node in visited)
return 0
visited.add(node)
path.add(node)
val colorIndex = colors[node] - 'a'
count[node][colorIndex] = 1
adj[node]?.forEach{ nei ->
if (dfs(nei) == Integer.MAX_VALUE)
return Integer.MAX_VALUE
(0 until 26).forEach{ c ->
count[node][c] = maxOf(
count[node][c],
count[nei][c] + if(c == colorIndex) 1 else 0
)
}
}
path.remove(node)
return count[node].max()!!
}
var res = 0
for (i in 0 until colors.length) {
res = maxOf(res, dfs(i))
}
return if(res == Integer.MAX_VALUE) -1 else res
}
}