Class Solution
- java.lang.Object
-
- g1801_1900.s1857_largest_color_value_in_a_directed_graph.Solution
-
public class Solution extends Object
1857 - Largest Color Value in a Directed Graph.Hard
There is a directed graph of
ncolored nodes andmedges. The nodes are numbered from0ton - 1.You are given a string
colorswherecolors[i]is a lowercase English letter representing the color of theithnode in this graph ( 0-indexed ). You are also given a 2D arrayedgeswhereedges[j] = [aj, bj]indicates that there is a directed edge from nodeajto nodebj.A valid path in the graph is a sequence of nodes
x1 -> x2 -> x3 -> … -> xksuch that there is a directed edge fromxitoxi+1for every1 <= 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
-1if 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:
n == colors.lengthm == edges.length1 <= n <= 1050 <= m <= 105colorsconsists of lowercase English letters.0 <= aj, bj < n
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
-
-
Method Detail
-
largestPathValue
public int largestPathValue(String colors, int[][] edges)
-
-