Hard
,Hash Table
,DP
,Graph
1857. Largest Color Value in a Directed Graph
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]
= [, ] indicates that there is a directed edge from node to node .
A valid path in the graph is a sequence of nodes x1 -> x2 -> x3 -> ... -> xk
such that there is a directed edge from to 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:
Example 2:
Constraints:
n
== colors.length
m
== edges.length
n
<= 105m
<= 105colors
consists of lowercase English letters.n