Skip to content

Commit db70a37

Browse files
committed
Sync LeetCode submission Runtime - 153 ms (15.12%), Memory - 123.8 MB (34.41%)
1 parent 11882be commit db70a37

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>
2+
3+
<p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p>
4+
5+
<p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p>
6+
7+
<p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p>
13+
14+
<pre>
15+
<strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]]
16+
<strong>Output:</strong> 3
17+
<strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p>
23+
24+
<pre>
25+
<strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]]
26+
<strong>Output:</strong> -1
27+
<strong>Explanation:</strong> There is a cycle from 0 to 0.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>n == colors.length</code></li>
35+
<li><code>m == edges.length</code></li>
36+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
37+
<li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li>
38+
<li><code>colors</code> consists of lowercase English letters.</li>
39+
<li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li>
40+
</ul>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
private int dfs(int node, String colors, Map<Integer, List<Integer>> adj, int[][] count,
3+
boolean[] visit, boolean[] inStack) {
4+
// If the node is already in the stack, we have a cycle.
5+
if (inStack[node]) {
6+
return Integer.MAX_VALUE;
7+
}
8+
if (visit[node]) {
9+
return count[node][colors.charAt(node) - 'a'];
10+
}
11+
// Mark the current node as visited and part of current recursion stack.
12+
visit[node] = true;
13+
inStack[node] = true;
14+
15+
if (adj.containsKey(node)) {
16+
for (int neighbor : adj.get(node)) {
17+
if (dfs(neighbor, colors, adj, count, visit, inStack) == Integer.MAX_VALUE) {
18+
return Integer.MAX_VALUE;
19+
}
20+
for (int i = 0; i < 26; i++) {
21+
count[node][i] = Math.max(count[node][i], count[neighbor][i]);
22+
}
23+
}
24+
}
25+
26+
// After all the incoming edges to the node are processed,
27+
// we count the color on the node itself.
28+
count[node][colors.charAt(node) - 'a']++;
29+
// Remove the node from the stack.
30+
inStack[node] = false;
31+
return count[node][colors.charAt(node) - 'a'];
32+
}
33+
34+
public int largestPathValue(String colors, int[][] edges) {
35+
int n = colors.length();
36+
Map<Integer, List<Integer>> adj = new HashMap<>();
37+
38+
for (int[] edge : edges) {
39+
adj.computeIfAbsent(edge[0], k->new ArrayList<Integer>()).add(edge[1]);
40+
}
41+
42+
int[][] count = new int[n][26];
43+
boolean[] visit = new boolean[n];
44+
boolean[] inStack = new boolean[n];
45+
int answer = 0;
46+
for (int i = 0; i < n; i++) {
47+
answer = Math.max(answer, dfs(i, colors, adj, count, visit, inStack));
48+
}
49+
50+
return answer == Integer.MAX_VALUE ? -1 : answer;
51+
}
52+
}

0 commit comments

Comments
 (0)