|
| 1 | +/** |
| 2 | + * 3372. Maximize the Number of Target Nodes After Connecting Trees I |
| 3 | + * https://leetcode.com/problems/maximize-the-number-of-target-nodes-after-connecting-trees-i/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * There exist two undirected trees with n and m nodes, with distinct labels in ranges [0, n - 1] |
| 7 | + * and [0, m - 1], 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. You are also given an integer k. |
| 13 | + * |
| 14 | + * Node u is target to node v if the number of edges on the path from u to v is less than or equal |
| 15 | + * to k. Note that 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 | + * target to node i of the first tree if you have to connect one node from the first tree to |
| 19 | + * another node in the second tree. |
| 20 | + * |
| 21 | + * Note that queries are independent from each other. That is, for every query you will remove |
| 22 | + * the added edge before proceeding to the next query. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number[][]} edges1 |
| 27 | + * @param {number[][]} edges2 |
| 28 | + * @param {number} k |
| 29 | + * @return {number[]} |
| 30 | + */ |
| 31 | +var maxTargetNodes = function(edges1, edges2, k) { |
| 32 | + const graph1 = buildGraph(edges1); |
| 33 | + const graph2 = buildGraph(edges2); |
| 34 | + const n = edges1.length + 1; |
| 35 | + const m = edges2.length + 1; |
| 36 | + |
| 37 | + let tree2Max = 0; |
| 38 | + if (k > 0) { |
| 39 | + tree2Max = Math.max(...Array.from({ length: m }, (_, i) => |
| 40 | + countReachable(graph2, i, k - 1) |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + const result = []; |
| 45 | + for (let i = 0; i < n; i++) { |
| 46 | + const tree1Count = countReachable(graph1, i, k); |
| 47 | + result.push(tree1Count + tree2Max); |
| 48 | + } |
| 49 | + |
| 50 | + return result; |
| 51 | + |
| 52 | + function buildGraph(edges) { |
| 53 | + const graph = {}; |
| 54 | + for (const [u, v] of edges) { |
| 55 | + if (!graph[u]) graph[u] = []; |
| 56 | + if (!graph[v]) graph[v] = []; |
| 57 | + graph[u].push(v); |
| 58 | + graph[v].push(u); |
| 59 | + } |
| 60 | + return graph; |
| 61 | + } |
| 62 | + |
| 63 | + function countReachable(graph, start, maxDist) { |
| 64 | + const queue = [start]; |
| 65 | + const visited = new Set([start]); |
| 66 | + let count = 1; |
| 67 | + let dist = 0; |
| 68 | + |
| 69 | + while (queue.length > 0 && dist < maxDist) { |
| 70 | + const size = queue.length; |
| 71 | + dist++; |
| 72 | + for (let i = 0; i < size; i++) { |
| 73 | + const node = queue.shift(); |
| 74 | + if (graph[node]) { |
| 75 | + for (const neighbor of graph[node]) { |
| 76 | + if (!visited.has(neighbor)) { |
| 77 | + visited.add(neighbor); |
| 78 | + queue.push(neighbor); |
| 79 | + count++; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return count; |
| 86 | + } |
| 87 | +}; |
0 commit comments