Largest Color Value in a Directed Graph - Problem
Graph Coloring Path Challenge: You're given a directed graph where each node has a specific color (represented by lowercase letters). Your mission is to find the most dominant color along any valid path in the graph.

The Problem: Given n colored nodes numbered 0 to n-1 and a string colors where colors[i] represents the color of node i, plus an array of directed edges, you need to:

1. Find all valid paths in the graph (following directed edges)
2. For each path, count the frequency of each color
3. The "color value" of a path is the count of its most frequent color
4. Return the maximum color value among all paths

Important: If the graph contains a cycle, return -1 (since cycles would allow infinite path lengths).

Example: If a path goes through nodes with colors "abbac", the most frequent color is 'a' (appears 2 times), so this path has color value 2.

Input & Output

example_1.py โ€” Basic Path
$ Input: colors = "abaca", edges = [[0,1],[0,2],[2,3],[3,4]]
โ€บ Output: 3
๐Ÿ’ก Note: The path 0 -> 2 -> 3 -> 4 produces the sequence "acaa". Color 'a' appears 3 times, which is the maximum.
example_2.py โ€” Multiple Paths
$ Input: colors = "a", edges = [[0,0]]
โ€บ Output: -1
๐Ÿ’ก Note: There is a cycle from 0 to 0, so we return -1.
example_3.py โ€” Single Node
$ Input: colors = "abc", edges = []
โ€บ Output: 1
๐Ÿ’ก Note: No edges means each node forms its own path. The maximum color value is 1 (any single node).

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค m โ‰ค 105
  • edges[i].length == 2
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • ai โ‰  bi
  • colors consists of lowercase English letters

Visualization

Tap to expand
๐Ÿ›๏ธ Museum Tour PlanningArt Room(Theme A)History(Theme B)Gallery(Theme A)Science(Theme C)Sculpture(Theme A)๐ŸŽฏ Optimal Tour Strategyโ€ข Step 1: Identify entrance rooms (no prerequisites)โ€ข Step 2: Check for circular routes that trap visitorsโ€ข Step 3: Calculate max theme exposure for each roomโ€ข Result: Best tour gives 3 Art experiences (Aโ†’Bโ†’Aโ†’Cโ†’A)MAX3A
Understanding the Visualization
1
Map the Museum
Create a map of all rooms and their connections, noting which rooms have no entrance requirements
2
Detect Circular Routes
Use topological sorting to ensure no visitor gets trapped in circular paths
3
Plan Optimal Routes
For each room, calculate the maximum theme exposure possible from all routes leading to it
4
Find Best Experience
Return the highest theme exposure value achievable across all possible museum tours
Key Takeaway
๐ŸŽฏ Key Insight: By processing museum rooms in dependency order (topological sort), we can efficiently calculate the maximum theme exposure without getting trapped in circular routes, ensuring every visitor gets the optimal museum experience.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
31.8K Views
Medium Frequency
~25 min Avg. Time
982 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen