[LeetCode] Largest Color Value in a Directed Graph

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.

  • topological sort solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int largestPathValue(string colors, vector<vector<int>>& edges) {
int n = colors.length();
vector<bool> vis(n, false);
vector<vector<int>> graph(n);
vector<vector<int>> history(n, vector<int>(26,0));
vector<int> counter(n,0);
int seen = 0;
int res = 0;
for(auto edge : edges) {
graph[edge[0]].push_back(edge[1]);
counter[edge[1]]++;
}
queue<int> q;
for(int i = 0; i < n; i++)
if(counter[i] == 0)
q.push(i);
while(!q.empty()) {
auto node = q.front(); q.pop();
if(vis[node]) continue;

seen++;
vis[node] = true;
history[node][colors[node]-'a']++;
res = max(res, *max_element(history[node].begin(), history[node].end()));
for(auto near : graph[node]) {
if(vis[near]) return -1;

if(--counter[near] == 0) q.push(near);
for(int i = 0; i < 26; i++) {
history[near][i] = max(history[near][i], history[node][i]);
}
}
}
return seen == n ? res : -1;

}
};
  • dfs solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
vector<bool> vis;
vector<vector<int>> graph;
bool cycle = false;
int res = 0;
vector<vector<int>> dp;
vector<bool> vis2;
vector<int> dfs(string& color, int node) {
if(cycle) return {};
if(vis2[node]) {
cycle = true;
return dp[node];
}
if(vis[node]) {
return dp[node];
}
vis[node] = true;
vis2[node] = true;
for(auto near : graph[node]) {
auto ans = dfs(color, near);
for(int i = 0; i < 26; i++) {
dp[node][i] = max(dp[node][i], ans[i]);
res = max(dp[node][i], res);
}
}
res = max(res, ++dp[node][color[node]-'a']);
vis2[node] = false;
return dp[node];
}
public:
int largestPathValue(string colors, vector<vector<int>>& edges) {
int n = colors.length();
vis = vector<bool>(n, false);
vis2 = vector<bool>(n, false);
dp = vector<vector<int>>(n, vector<int>(26));
graph = vector<vector<int>>(n);
for(auto edge : edges) {
graph[edge[0]].push_back(edge[1]);
}
for(int i = 0; i < n and !cycle; i++) {
if(!vis[i]) {
dfs(colors, i);
}
}

return cycle ? -1 : res;

}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/07/PS/LeetCode/largest-color-value-in-a-directed-graph/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.