Skip to content

Latest commit

 

History

History
89 lines (74 loc) · 3.28 KB

File metadata and controls

89 lines (74 loc) · 3.28 KB

1857. Largest Color Value in a Directed Graph

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 <= 105
  • 0 <= m <= 105
  • colors consists of lowercase English letters.
  • 0 <= aj, bj < n

Solutions (Rust)

1. Solution

impl Solution {
    pub fn largest_path_value(colors: String, edges: Vec<Vec<i32>>) -> i32 {
        let colors = colors
            .bytes()
            .map(|c| (c - b'a') as usize)
            .collect::<Vec<_>>();
        let n = colors.len();
        let mut indegree = vec![0; n];
        let mut next_nodes = vec![vec![]; n];
        let mut nodes = vec![];
        let mut node_colors = vec![[0; 26]; n];
        let mut count = 0;

        for edge in &edges {
            indegree[edge[1] as usize] += 1;
            next_nodes[edge[0] as usize].push(edge[1] as usize);
        }

        for i in 0..n {
            if indegree[i] == 0 {
                nodes.push(i);
            }
            node_colors[i][colors[i]] += 1;
        }

        while let Some(i) = nodes.pop() {
            count += 1;

            for &j in &next_nodes[i] {
                indegree[j] -= 1;
                if indegree[j] == 0 {
                    nodes.push(j);
                }

                for k in 0..26 {
                    if k != colors[j] {
                        node_colors[j][k] = node_colors[j][k].max(node_colors[i][k]);
                    } else {
                        node_colors[j][k] = node_colors[j][k].max(node_colors[i][k] + 1);
                    }
                }
            }
        }

        if count < n {
            return -1;
        }

        (0..n).map(|i| node_colors[i][colors[i]]).max().unwrap()
    }
}