Tap here to switch tabs
Problem
Submissions

Problem: Largest Color Value in a Directed Graph

med
30 min
Try to solve the Largest Color Value in a Directed Graph problem.

Statement

There is a directed graph consisting of nn colored nodes labeled from 00 to n−1n - 1 and mm directed edges. You are given a string colors of length nn, where colors[i] represents the color of the iextthi^{ ext{th}} node, and a two dimensional array edges where edges[j] = [a_j, b_j] indicates a directed edge from node aja_j to node bjb_j.

A path in the graph is a sequence of nodes u1ou2o…ouku_1 o u_2 o \dots o u_k such that there exists a directed edge from uiu_i to ui+1u_{i+1} for all 1≤i<k1 \leq i < k. The color value of a path is defined as the frequency of the most common color appearing among the nodes visited in that path.

Return the maximum color value among all valid paths in the graph. If the graph contains a cycle, return −1-1.

Constraints:

  • n ==== colors.length

  • m ==== edges.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 0≤0 \leq m ≤105\leq 10^5

  • colors consists of lowercase English letters.

  • 0≤aj,bj<0 \leq a_j, b_j < n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Largest Color Value in a Directed Graph

med
30 min
Try to solve the Largest Color Value in a Directed Graph problem.

Statement

There is a directed graph consisting of nn colored nodes labeled from 00 to n−1n - 1 and mm directed edges. You are given a string colors of length nn, where colors[i] represents the color of the iextthi^{ ext{th}} node, and a two dimensional array edges where edges[j] = [a_j, b_j] indicates a directed edge from node aja_j to node bjb_j.

A path in the graph is a sequence of nodes u1ou2o…ouku_1 o u_2 o \dots o u_k such that there exists a directed edge from uiu_i to ui+1u_{i+1} for all 1≤i<k1 \leq i < k. The color value of a path is defined as the frequency of the most common color appearing among the nodes visited in that path.

Return the maximum color value among all valid paths in the graph. If the graph contains a cycle, return −1-1.

Constraints:

  • n ==== colors.length

  • m ==== edges.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 0≤0 \leq m ≤105\leq 10^5

  • colors consists of lowercase English letters.

  • 0≤aj,bj<0 \leq a_j, b_j < n