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:
n == colors.lengthm == edges.length1 <= n <= 1050 <= m <= 105colors consists of lowercase English letters.0 <= aj, bj < nThis approach leverages a topological sort using Kahn's algorithm to process nodes in an order that respects the directed edges. We use an additional data structure to keep a running tally of the frequency of each color at every node, and update these tallies as we process nodes. This allows us to track the most frequently occurring color for all paths.
If we detect any cycles during this process, we can immediately return -1.
The implementation starts by constructing an adjacency list from the edges and calculates the in-degree for each node. We use Kahn's algorithm to perform a topological sort, starting with nodes with zero in-degree.
During the processing, we maintain a color frequency table for each node, updating the table as we process each node's neighbors. If the node count at the end doesn't match the number of nodes, a cycle exists, and we return -1. Otherwise, we return the maximum color frequency value found.
Java
Time Complexity: O(n + m), where n is the number of nodes and m is the number of edges. This accounts for the initial processing of nodes and edges and the BFS traversal of the graph.
Space Complexity: O(n), mainly due to the adjacency list, in-degree array, and color count table.
This approach applies a Depth-First Search (DFS) on each node while using memoization to store and retrieve previously computed results. This aids in finding the most frequent color along paths derived from each node.
During DFS, we also check for cycles by marking nodes as currently being visited, immediately returning -1 upon detecting a cycle.
The C++ solution utilizes DFS to explore paths from all nodes. During exploration, if a node is detected as currently being visited, it indicates a cycle, and we return -1.
A memoization table stores the frequency of colors for paths from each node. After processing all nodes, the highest frequency from the table is the desired result.
JavaScript
Time Complexity: O(n + m), for processing nodes, edges, and DFS traversal.
Space Complexity: O(n), driven by memoization and state tracking.
| Approach | Complexity |
|---|---|
| Topological Sort with Kahn's Algorithm and Color Frequency Tracking | Time Complexity: O(n + m), where n is the number of nodes and m is the number of edges. This accounts for the initial processing of nodes and edges and the BFS traversal of the graph. Space Complexity: O(n), mainly due to the adjacency list, in-degree array, and color count table. |
| DFS with Cycle Detection and Memoization | Time Complexity: O(n + m), for processing nodes, edges, and DFS traversal. Space Complexity: O(n), driven by memoization and state tracking. |
Largest Color Value in a Directed Graph - Leetcode 1857 - Python • NeetCodeIO • 22,081 views views
Watch 9 more video solutions →Practice Largest Color Value in a Directed Graph with our built-in code editor and test cases.
Practice on FleetCode