1857. Largest Color Value in a Directed Graph

Largest Color Value in a Directed Graph (LeetCode 1857) is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern.

How to solve Largest Color Value in a Directed Graph

Largest Color Value in a Directed Graph is a Topological Sort problem, part of Graph Traversal Patterns (DFS & BFS). Master topological sorting using Kahn's BFS algorithm. Solve course schedule and build order problems.

Work towards the solution in these steps:

  1. Use topological sort.
  2. let dp[u][c] := the maximum count of vertices with color c of any path starting from vertex u. (by JerryJin2905)

The reference solution runs in O(n + m) time and O(n + m) space.

The Largest Color Value in a Directed Graph solution and editorial walks through the full approach with reference code.

Problem statement

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.length

- m == edges.length

- 1 <= n <= 10^5

- 0 <= m <= 10^5

- colors consists of lowercase English letters.

- 0 <= aj, bj < n

Topics and companies

Topics: Hash Table, Dynamic Programming, Graph, Topological Sort, Memoization, Counting.

How to practise Largest Color Value in a Directed Graph on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n + m) time and O(n + m) space.

Open Largest Color Value in a Directed Graph in the code editor, or read the Largest Color Value in a Directed Graph editorial for a worked solution with its approach and complexity analysis.

Where Largest Color Value in a Directed Graph sits in the DSA pattern sheet

Problems related to Largest Color Value in a Directed Graph

Other problems that use the same Graph BFS - Topological Sort (Kahn's Algorithm) and Graph Traversal Patterns (DFS & BFS) techniques:

Largest Color Value in a Directed Graph FAQ

What is the LeetCode number of Largest Color Value in a Directed Graph?

Largest Color Value in a Directed Graph is LeetCode problem 1857, rated Hard.

What pattern is used to solve Largest Color Value in a Directed Graph?

Largest Color Value in a Directed Graph is a Topological Sort problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph BFS - Topological Sort (Kahn's Algorithm) subpattern.

What is the time complexity of Largest Color Value in a Directed Graph?

The reference solution runs in O(n + m) time and O(n + m) space.

Preparing this workspace.