> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Color Value in a Directed Graph

> Tested Python solution for LeetCode 1857 with 27 pytest cases. Generate a practice environment with lcpy.

LeetCode 1857, [Hard](/catalog/hard). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Graph Theory](/catalog/topics/graph-theory), [Topological Sort](/catalog/topics/topological-sort), [Memoization](/catalog/topics/memoization), [Counting](/catalog/topics/counting), Directed Acyclic Graph. [View on LeetCode](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/description/).

Generate this problem as a practice environment: tested reference solution, 27 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1857   # by problem number
lcpy gen -s largest_color_value_in_a_directed_graph   # by problem name
```

## Problem

There is a \<strong>directed graph\</strong> of \<code>n\</code> colored nodes and \<code>m\</code> edges. The nodes are numbered from \<code>0\</code> to \<code>n - 1\</code>.

You are given a string \<code>colors\</code> where \<code>colors\[i]\</code> is a lowercase English letter representing the \<strong>color\</strong> of the \<code>i\<sup>th\</sup>\</code> node in this graph (\<strong>0-indexed\</strong>). You are also given a 2D array \<code>edges\</code> where \<code>edges\[j] = \[a\<sub>j\</sub>, b\<sub>j\</sub>]\</code> indicates that there is a \<strong>directed edge\</strong> from node \<code>a\<sub>j\</sub>\</code> to node \<code>b\<sub>j\</sub>\</code>.

A valid \<strong>path\</strong> in the graph is a sequence of nodes \<code>x\<sub>1\</sub> -> x\<sub>2\</sub> -> x\<sub>3\</sub> -> ... -> x\<sub>k\</sub>\</code> such that there is a directed edge from \<code>x\<sub>i\</sub>\</code> to \<code>x\<sub>i+1\</sub>\</code> for every \<code>1 \<= i \< k\</code>. The \<strong>color value\</strong> of the path is the number of nodes that are colored the \<strong>most frequently\</strong> occurring color along that path.

Return \<em>the \<strong>largest color value\</strong> of any valid path in the given graph, or \</em>\<code>-1\</code>\<em> if the graph contains a cycle\</em>.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/04/21/leet1.png)

```
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](https://assets.leetcode.com/uploads/2021/04/21/leet2.png)

```
Input: colors = "a", edges = [[0,0]]
Output: -1
```

**Explanation:** There is a cycle from 0 to 0.

### Constraints

* `n == colors.length`
* `m == edges.length`
* `1 <= n <= 10^5`
* `0 <= m <= 10^5`
* `colors` consists of lowercase English letters.
* `0 <= aj, bj < n`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_color_value_in_a_directed_graph/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_color_value_in_a_directed_graph/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m) with a constant factor of 26 colors
    # Space: O(n)
    def largest_path_value(self, colors: str, edges: list[list[int]]) -> int:
        n = len(colors)
        adj: list[list[int]] = [[] for _ in range(n)]
        indegree = [0] * n
        for src, dst in edges:
            adj[src].append(dst)
            indegree[dst] += 1

        counts = [[0] * 26 for _ in range(n)]
        for node in range(n):
            counts[node][ord(colors[node]) - 97] = 1
        queue = [node for node in range(n) if indegree[node] == 0]
        processed = 0
        best = 0

        while queue:
            node = queue.pop()
            processed += 1
            node_counts = counts[node]
            local_best = max(node_counts)
            if local_best > best:
                best = local_best
            for nxt in adj[node]:
                nxt_counts = counts[nxt]
                nxt_color = ord(colors[nxt]) - 97
                for c in range(26):
                    cand = node_counts[c] + (1 if c == nxt_color else 0)
                    if cand > nxt_counts[c]:
                        nxt_counts[c] = cand
                indegree[nxt] -= 1
                if indegree[nxt] == 0:
                    queue.append(nxt)

        return best if processed == n else -1
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) with a constant factor of 26 colors | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.