Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa7a6f2

Browse files
committed
Add solution #3373
1 parent b1b9816 commit aa7a6f2

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,7 @@
20442044
3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium|
20452045
3362|[Zero Array Transformation III](./solutions/3362-zero-array-transformation-iii.js)|Medium|
20462046
3372|[Maximize the Number of Target Nodes After Connecting Trees I](./solutions/3372-maximize-the-number-of-target-nodes-after-connecting-trees-i.js)|Medium|
2047+
3373|[Maximize the Number of Target Nodes After Connecting Trees II](./solutions/3373-maximize-the-number-of-target-nodes-after-connecting-trees-ii.js)|Hard|
20472048
3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy|
20482049
3392|[Count Subarrays of Length Three With a Condition](./solutions/3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
20492050
3394|[Check if Grid can be Cut into Sections](./solutions/3394-check-if-grid-can-be-cut-into-sections.js)|Medium|
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* 3373. Maximize the Number of Target Nodes After Connecting Trees II
3+
* https://leetcode.com/problems/maximize-the-number-of-target-nodes-after-connecting-trees-ii/
4+
* Difficulty: Hard
5+
*
6+
* There exist two undirected trees with n and m nodes, labeled from [0, n - 1] and [0, m - 1],
7+
* respectively.
8+
*
9+
* You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively,
10+
* where edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first
11+
* tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the
12+
* second tree.
13+
*
14+
* Node u is target to node v if the number of edges on the path from u to v is even. Note that
15+
* a node is always target to itself.
16+
*
17+
* Return an array of n integers answer, where answer[i] is the maximum possible number of nodes
18+
* that are target to node i of the first tree if you had to connect one node from the first tree
19+
* to another node in the second tree.
20+
*
21+
* Note that queries are independent from each other. That is, for every query you will remove the
22+
* added edge before proceeding to the next query.
23+
*/
24+
25+
/**
26+
* @param {number[][]} edges1
27+
* @param {number[][]} edges2
28+
* @return {number[]}
29+
*/
30+
var maxTargetNodes = function(edges1, edges2) {
31+
const graph1 = buildGraph(edges1);
32+
const graph2 = buildGraph(edges2);
33+
const n = graph1.length;
34+
const { color: color1 } = getBipartiteGroups(graph1);
35+
const { maxGroup: maxGroup2 } = getBipartiteGroups(graph2);
36+
const group0Count = color1.filter(c => c === 0).length;
37+
const group1Count = n - group0Count;
38+
const result = new Array(n);
39+
40+
for (let i = 0; i < n; i++) {
41+
result[i] = (color1[i] === 0 ? group0Count : group1Count) + maxGroup2;
42+
}
43+
44+
return result;
45+
46+
function buildGraph(edges) {
47+
const n = edges.length + 1;
48+
const graph = Array.from({ length: n }, () => []);
49+
for (const [u, v] of edges) {
50+
graph[u].push(v);
51+
graph[v].push(u);
52+
}
53+
return graph;
54+
}
55+
56+
function getBipartiteGroups(graph) {
57+
const n = graph.length;
58+
const color = new Array(n).fill(-1);
59+
const groups = [0, 0];
60+
61+
for (let i = 0; i < n; i++) {
62+
if (color[i] === -1) {
63+
dfs(i, 0);
64+
}
65+
}
66+
67+
return { color, maxGroup: Math.max(groups[0], groups[1]) };
68+
69+
function dfs(node, c) {
70+
color[node] = c;
71+
groups[c]++;
72+
for (const neighbor of graph[node]) {
73+
if (color[neighbor] === -1) {
74+
dfs(neighbor, 1 - c);
75+
}
76+
}
77+
}
78+
}
79+
};

0 commit comments

Comments
 (0)