Skip to content

Commit 1e09ea9

Browse files
committed
leetcode
1 parent f0bd7e5 commit 1e09ea9

5 files changed

+304
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
3+
4+
-* 2246. Longest Path With Different Adjacent Characters *-
5+
6+
7+
You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.
8+
9+
You are also given a string s of length n, where s[i] is the character assigned to node i.
10+
11+
Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.
12+
13+
14+
15+
Example 1:
16+
17+
18+
Input: parent = [-1,0,0,1,1,2], s = "abacbe"
19+
Output: 3
20+
Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.
21+
It can be proven that there is no longer path that satisfies the conditions.
22+
Example 2:
23+
24+
25+
Input: parent = [-1,0,0,0], s = "aabc"
26+
Output: 3
27+
Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.
28+
29+
30+
Constraints:
31+
32+
n == parent.length == s.length
33+
1 <= n <= 105
34+
0 <= parent[i] <= n - 1 for all i >= 1
35+
parent[0] == -1
36+
parent represents a valid tree.
37+
s consists of only lowercase English letters.
38+
39+
40+
41+
42+
43+
44+
*/
45+
46+
import 'dart:collection';
47+
import 'dart:math';
48+
49+
class Node {
50+
String c;
51+
List<Node> next = [];
52+
Node(this.c);
53+
54+
void add(Node n) {
55+
n.next.add(this);
56+
}
57+
}
58+
59+
class A {
60+
int maxi = 0;
61+
int longestPath(List<int> parent, String s) {
62+
List<Node> list = List.generate(parent.length, (i) => Node(s[i]));
63+
for (int i = 1; i < list.length; i++) {
64+
list[i].add(list[parent[i]]);
65+
}
66+
iterate(list[0]);
67+
return maxi;
68+
}
69+
70+
int iterate(Node root) {
71+
if (root == null) {
72+
return 0;
73+
}
74+
int myMax = 1;
75+
for (Node n in root.next) {
76+
int count = iterate(n);
77+
if (n.c != root.c) {
78+
if (myMax != 1) {
79+
maxi = max(myMax + count, maxi);
80+
}
81+
myMax = max(count + 1, myMax);
82+
}
83+
}
84+
maxi = max(myMax, maxi);
85+
return myMax;
86+
}
87+
}
88+
89+
class Pair {
90+
int count, maxCount;
91+
Pair(this.count, this.maxCount);
92+
}
93+
94+
class B {
95+
int longestPath(List<int> parent, String s) {
96+
List<List<int>> a = List.generate(parent.length, (_) => []);
97+
for (int i = 1; i < parent.length; i++) {
98+
a[parent[i]].add(i);
99+
}
100+
Pair p = dfs(0, a, s);
101+
return p.maxCount;
102+
}
103+
104+
Pair dfs(int root, List<List<int>> a, String s) {
105+
if (a[root].length == 0) return Pair(1, 1);
106+
int maxCount = 0, count1 = 0, count2 = 0;
107+
for (int child in a[root]) {
108+
Pair pair = dfs(child, a, s);
109+
maxCount = max(maxCount, pair.maxCount);
110+
if (s[root] != s[child]) {
111+
if (count1 < pair.count) {
112+
count2 = count1;
113+
count1 = pair.count;
114+
} else if (pair.count > count2) {
115+
count2 = pair.count;
116+
}
117+
}
118+
}
119+
maxCount = max(maxCount, count1 + count2 + 1);
120+
return Pair(count1 + 1, maxCount);
121+
}
122+
}
123+
124+
class Solution {
125+
Map<List<int>, int> _cache = {};
126+
int longest(Map<int, List<int>> tree, int root, String s, bool branch) {
127+
int l = 1;
128+
List<int> key = [root, branch ? 1 : 0];
129+
if (_cache.containsKey(key)) {
130+
return _cache[key] ?? 0;
131+
}
132+
if (branch) {
133+
int best = 0;
134+
int sbest = 0;
135+
for (int j in tree[root] ?? []) {
136+
if (s[root] != s[j]) {
137+
int currl = longest(tree, j, s, false);
138+
List<int> temp = [sbest, best, currl];
139+
temp.sort();
140+
sbest = temp[1];
141+
best = temp[2];
142+
}
143+
}
144+
l = max(l, 1 + best + sbest);
145+
_cache[key] = l;
146+
return l;
147+
}
148+
for (int j in tree[root] ?? []) {
149+
if (s[root] != s[j]) {
150+
int currl = longest(tree, j, s, false);
151+
l = max(l, 1 + currl);
152+
}
153+
}
154+
_cache[key] = l;
155+
return l;
156+
}
157+
158+
int longestPath(List<int> parent, String s) {
159+
int n = s.length;
160+
_cache = {};
161+
Map<int, List<int>> tree = Map<int, List<int>>();
162+
for (int i = 0; i < n; i++) {
163+
if (parent[i] != -1) {
164+
tree.putIfAbsent(parent[i], () => []);
165+
tree[parent[i]]?.add(i);
166+
}
167+
}
168+
int mpath = 0;
169+
for (int i = 0; i < n; i++) {
170+
int l = longest(tree, i, s, true);
171+
mpath = max(mpath, l);
172+
}
173+
return mpath;
174+
}
175+
}
176+
177+
class D {
178+
int longestPath(List<int> parent, String s) {
179+
int n = parent.length, res = 1;
180+
List<int> cnt = List.filled(n, 0),
181+
top1 = List.filled(n, 1),
182+
top2 = List.filled(n, 1);
183+
for (int i = 1; i < n; ++i) ++cnt[parent[i]];
184+
Queue<int> q = Queue();
185+
for (int i = 1; i < n; ++i) if (cnt[i] == 0) q.add(i);
186+
while (q.isNotEmpty && q.first != 0) {
187+
int i = q.first;
188+
int p = parent[i];
189+
q.removeFirst();
190+
int length = 0;
191+
if (length == 1 + (s[i] != s[p] ? top1[i] : 0)) {
192+
top1[p] <= length;
193+
top2[p] = top1[p];
194+
top1[p] = length;
195+
} else
196+
top2[p] = max(top2[p], length);
197+
if (--cnt[p] == 0) {
198+
q.add(p);
199+
res = max(res, top1[p] + top2[p] - 1);
200+
}
201+
}
202+
return res;
203+
}
204+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "sort"
4+
5+
func longestPath(parent []int, s string) (max int) {
6+
gr := make([][]int, len(parent))
7+
for ch := 1; ch < len(parent); ch++ {
8+
gr[parent[ch]] = append(gr[parent[ch]], ch)
9+
}
10+
dfs(0, gr, []byte(s), &max)
11+
return
12+
}
13+
14+
func dfs(cur int, gr [][]int, s []byte, max *int) int {
15+
maxpq := []int{}
16+
for _, ch := range gr[cur] {
17+
l := dfs(ch, gr, s, max)
18+
if s[cur] != s[ch] {
19+
maxpq = append(maxpq, l)
20+
}
21+
}
22+
sort.Ints(maxpq)
23+
locmax, retmax := 1, 1
24+
if len(maxpq) > 0 {
25+
locmax += maxpq[len(maxpq)-1]
26+
retmax += maxpq[len(maxpq)-1]
27+
}
28+
if len(maxpq) > 1 {
29+
locmax += maxpq[len(maxpq)-2]
30+
}
31+
32+
if *max < locmax {
33+
*max = locmax
34+
}
35+
36+
return retmax
37+
}

LongestPathWithDifferentAdjacentCharacters/longest_path_with_Different_adjacent_characters.md

Whitespace-only changes.

NumberOfNodesInTheSub-TreeWithTheSameLabel/number_of_nodes_in_the_sub_tree_with_the_same_label.dart

+62-62
Original file line numberDiff line numberDiff line change
@@ -184,68 +184,68 @@ class C {
184184

185185
//================
186186

187-
class ANode {
188-
late String c;
189-
late int key;
190-
late List<Node> cs;
191-
late Node? p;
192-
Node(int key) {
193-
this.key = key;
194-
cs = <Node>[];
195-
this.p = null;
196-
}
197-
}
198-
199-
class D {
200-
late List<int> res;
201-
Node build(int n, List<List<int>> es, String l) {
202-
HashMap<int, List<int>> g = HashMap();
203-
for (List<int> e in es) {
204-
g.putIfAbsent(e[0], () => []);
205-
g.putIfAbsent(e[1], () => []);
206-
g[e[1]]?.add(e[0]);
207-
g[e[0]]?.add(e[1]);
208-
}
209-
Node root = Node(0);
210-
root.c = l[0];
211-
Queue<Node> q = Queue();
212-
q.add(root);
213-
while (!q.isEmpty) {
214-
Node current = q.removeLast();
215-
for (int neighbor in g[current.key] ?? []) {
216-
if (current.p == null || neighbor != current.p?.key) {
217-
Node next = Node(neighbor);
218-
next.c = l[neighbor];
219-
next.p = current;
220-
current.cs.add(next);
221-
q.add(next);
222-
}
223-
}
224-
}
225-
return root;
226-
}
227-
228-
HashMap<String, int> dfs(Node? node) {
229-
HashMap<String, int> map = HashMap();
230-
if (node == null) return map;
231-
map[node.c] = 1;
232-
for (Node child in node.cs) {
233-
HashMap<String, int> cHashMap = dfs(child);
234-
for (String c in cHashMap.keys) {
235-
map[c] = (map[c] ?? 0) + cHashMap[c]!;
236-
}
237-
}
238-
res[node.key] = map[node.c]!;
239-
return map;
240-
}
241-
242-
List<int> countSubTrees(int n, List<List<int>> edges, String labels) {
243-
Node root = build(n, edges, labels);
244-
res = List.filled(n, 0, growable: true);
245-
dfs(root);
246-
return res;
247-
}
248-
}
187+
// class ANode {
188+
// late String c;
189+
// late int key;
190+
// late List<Node> cs;
191+
// late Node? p;
192+
// Node(int key) {
193+
// this.key = key;
194+
// cs = <Node>[];
195+
// this.p = null;
196+
// }
197+
// }
198+
199+
// class D {
200+
// late List<int> res;
201+
// Node build(int n, List<List<int>> es, String l) {
202+
// HashMap<int, List<int>> g = HashMap();
203+
// for (List<int> e in es) {
204+
// g.putIfAbsent(e[0], () => []);
205+
// g.putIfAbsent(e[1], () => []);
206+
// g[e[1]]?.add(e[0]);
207+
// g[e[0]]?.add(e[1]);
208+
// }
209+
// Node root = Node(0);
210+
// root.c = l[0];
211+
// Queue<Node> q = Queue();
212+
// q.add(root);
213+
// while (!q.isEmpty) {
214+
// Node current = q.removeLast();
215+
// for (int neighbor in g[current.key] ?? []) {
216+
// if (current.p == null || neighbor != current.p?.key) {
217+
// Node next = Node(neighbor);
218+
// next.c = l[neighbor];
219+
// next.p = current;
220+
// current.cs.add(next);
221+
// q.add(next);
222+
// }
223+
// }
224+
// }
225+
// return root;
226+
// }
227+
228+
// HashMap<String, int> dfs(Node? node) {
229+
// HashMap<String, int> map = HashMap();
230+
// if (node == null) return map;
231+
// map[node.c] = 1;
232+
// for (Node child in node.cs) {
233+
// HashMap<String, int> cHashMap = dfs(child);
234+
// for (String c in cHashMap.keys) {
235+
// map[c] = (map[c] ?? 0) + cHashMap[c]!;
236+
// }
237+
// }
238+
// res[node.key] = map[node.c]!;
239+
// return map;
240+
// }
241+
242+
// List<int> countSubTrees(int n, List<List<int>> edges, String labels) {
243+
// Node root = build(n, edges, labels);
244+
// res = List.filled(n, 0, growable: true);
245+
// dfs(root);
246+
// return res;
247+
// }
248+
// }
249249

250250
class E {
251251
// Time limit exceed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
182182
- [**149.** Max Points on a Line](MaxPointsOnALine/max_points_on_a_line.dart)
183183
- [**1443.** Minimum Time to Collect All Apples in a Tree](MinimumTimeToCollectAllApplesInATree/minimum_time_to_collect_all_apples_in_a_tree.dart)
184184
- [**1519.** Number of Nodes in the Sub-Tree With the Same Label](NumberOfNodesInTheSub-TreeWithTheSameLabel/number_of_nodes_in_the_sub_tree_with_the_same_label.dart)
185+
- [**2246.** Longest Path With Different Adjacent Characters](LongestPathWithDifferentAdjacentCharacters/longest_path_with_Different_adjacent_characters.dart)
185186

186187
## Reach me via
187188

0 commit comments

Comments
 (0)