-
Notifications
You must be signed in to change notification settings - Fork 1
/
2246_Longest_Path_With_Different_Adjacent_Characters.cpp
69 lines (52 loc) · 1.47 KB
/
2246_Longest_Path_With_Different_Adjacent_Characters.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// https://leetcode.com/problems/longest-path-with-different-adjacent-characters/description/
// https://leetcode.com/problems/longest-path-with-different-adjacent-characters/discuss/3044386/c-picture-commented-solution-dfs-on-adj-list
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj;
int ans = 1;
int dfs(int currNode, int parent, string &labels)
{
int maxPath = 1;
for (int &node : adj[currNode])
{
if (node != parent)
{ // as adj list contains a edge to parent node as well, so to avoid an infinite loop
int path = dfs(node, currNode, labels);
if (labels[node] != labels[currNode])
{ // adjacent chars are not equal
ans = max(ans, maxPath + path);
maxPath = max(maxPath, path + 1);
}
}
}
return maxPath;
}
int longestPath(vector<int> &parent, string s)
{
// creating adjacency list
int n = parent.size();
adj.resize(n);
for (int i = 1; i < n; ++i)
{
adj[parent[i]].push_back(i); // i is bonded with its parent
adj[i].push_back(parent[i]); // undirected, that's why 2 way binding
}
/*
for ex1 - adj list =>
0 -> 1, 2
1 -> 0, 3, 4
2 -> 5
3 -> 1
4 -> 1
*/
dfs(0, -1, s);
return ans;
}
int main()
{
vector<int> parent = {-1, 0, 0, 1, 1, 2};
string s = "abacbe";
cout << longestPath(parent, s) << "\n";
return 0;
}