Skip to content

Commit 0e5dc8f

Browse files
Sync LeetCode submission Runtime - 590 ms (24.56%), Memory - 264.8 MB (12.28%)
1 parent 85f7cfd commit 0e5dc8f

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>
2+
3+
<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to node <code>i</code>.</p>
4+
5+
<p>Return <em>the length of the <strong>longest path</strong> in the tree such that no pair of <strong>adjacent</strong> nodes on the path have the same character assigned to them.</em></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/testingdrawio.png" style="width: 201px; height: 241px;" />
10+
<pre>
11+
<strong>Input:</strong> parent = [-1,0,0,1,1,2], s = &quot;abacbe&quot;
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -&gt; 1 -&gt; 3. The length of this path is 3, so 3 is returned.
14+
It can be proven that there is no longer path that satisfies the conditions.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/graph2drawio.png" style="width: 201px; height: 221px;" />
19+
<pre>
20+
<strong>Input:</strong> parent = [-1,0,0,0], s = &quot;aabc&quot;
21+
<strong>Output:</strong> 3
22+
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters is the path: 2 -&gt; 0 -&gt; 3. The length of this path is 3, so 3 is returned.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>n == parent.length == s.length</code></li>
30+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
31+
<li><code>0 &lt;= parent[i] &lt;= n - 1</code> for all <code>i &gt;= 1</code></li>
32+
<li><code>parent[0] == -1</code></li>
33+
<li><code>parent</code> represents a valid tree.</li>
34+
<li><code>s</code> consists of only lowercase English letters.</li>
35+
</ul>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int longestPath(vector<int>& parent, string s) {
4+
int n = parent.size();
5+
unordered_map<int, vector<int>> tree;
6+
for (int i=0; i<n; i++) tree[parent[i]].push_back(i);
7+
8+
int ans = 1;
9+
10+
auto dfs = [&](auto&& self, int node) -> pair<char, int> {
11+
vector<pair<char, int>> paths;
12+
for (int neigh : tree[node]) paths.push_back(self(self, neigh));
13+
14+
int maxLen = 1;
15+
for (auto path : paths) {
16+
char prev = path.first;
17+
int len = path.second;
18+
if (prev == s[node]) continue;
19+
20+
ans = max(ans, maxLen + len);
21+
maxLen = max(maxLen, len + 1);
22+
}
23+
24+
return {s[node], maxLen};
25+
};
26+
27+
dfs(dfs, 0);
28+
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)