Skip to content

Commit 65b12eb

Browse files
committed
feat: add solution 2003. Smallest Missing Genetic Value in Each Subtree
1 parent 04e9ff0 commit 65b12eb

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [2003. Smallest Missing Genetic Value in Each Subtree](https://leetcode.com/problems/smallest-missing-genetic-value-in-each-subtree)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>There is a <strong>family tree</strong> rooted at <code>0</code> consisting of <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> integer array <code>parents</code>, where <code>parents[i]</code> is the parent for node <code>i</code>. Since node <code>0</code> is the <strong>root</strong>, <code>parents[0] == -1</code>.</p>
6+
7+
<p>There are <code>10<sup>5</sup></code> genetic values, each represented by an integer in the <strong>inclusive</strong> range <code>[1, 10<sup>5</sup>]</code>. You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is a <strong>distinct </strong>genetic value for node <code>i</code>.</p>
8+
9+
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>n</code><em> where </em><code>ans[i]</code><em> is</em> <em>the <strong>smallest</strong> genetic value that is <strong>missing</strong> from the subtree rooted at node</em> <code>i</code>.</p>
10+
11+
<p>The <strong>subtree</strong> rooted at a node <code>x</code> contains node <code>x</code> and all of its <strong>descendant</strong> nodes.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/23/case-1.png" style="width: 204px; height: 167px;">
16+
<pre><strong>Input:</strong> parents = [-1,0,0,2], nums = [1,2,3,4]
17+
<strong>Output:</strong> [5,1,1,1]
18+
<strong>Explanation:</strong> The answer for each subtree is calculated as follows:
19+
- 0: The subtree contains nodes [0,1,2,3] with values [1,2,3,4]. 5 is the smallest missing value.
20+
- 1: The subtree contains only node 1 with value 2. 1 is the smallest missing value.
21+
- 2: The subtree contains nodes [2,3] with values [3,4]. 1 is the smallest missing value.
22+
- 3: The subtree contains only node 3 with value 4. 1 is the smallest missing value.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/23/case-2.png" style="width: 247px; height: 168px;">
27+
<pre><strong>Input:</strong> parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3]
28+
<strong>Output:</strong> [7,1,1,4,2,1]
29+
<strong>Explanation:</strong> The answer for each subtree is calculated as follows:
30+
- 0: The subtree contains nodes [0,1,2,3,4,5] with values [5,4,6,2,1,3]. 7 is the smallest missing value.
31+
- 1: The subtree contains nodes [1,2] with values [4,6]. 1 is the smallest missing value.
32+
- 2: The subtree contains only node 2 with value 6. 1 is the smallest missing value.
33+
- 3: The subtree contains nodes [3,4,5] with values [2,1,3]. 4 is the smallest missing value.
34+
- 4: The subtree contains only node 4 with value 1. 2 is the smallest missing value.
35+
- 5: The subtree contains only node 5 with value 3. 1 is the smallest missing value.
36+
</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre><strong>Input:</strong> parents = [-1,2,3,0,2,4,1], nums = [2,3,4,5,6,7,8]
41+
<strong>Output:</strong> [1,1,1,1,1,1,1]
42+
<strong>Explanation:</strong> The value 1 is missing from all the subtrees.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>n == parents.length == nums.length</code></li>
50+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= parents[i] &lt;= n - 1</code> for <code>i != 0</code></li>
52+
<li><code>parents[0] == -1</code></li>
53+
<li><code>parents</code> represents a valid tree.</li>
54+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
55+
<li>Each <code>nums[i]</code> is distinct.</li>
56+
</ul>
57+
</div>
58+
59+
<p>&nbsp;</p>
60+
61+
## Solutions
62+
63+
**Solution: `Depth-First Search`**
64+
65+
- Time complexity: <em>O(n)</em>
66+
- Space complexity: <em>O(n)</em>
67+
68+
<p>&nbsp;</p>
69+
70+
### **JavaScript**
71+
72+
```js
73+
/**
74+
* @param {number[]} parents
75+
* @param {number[]} nums
76+
* @return {number[]}
77+
*/
78+
const smallestMissingValueSubtree = function (parents, nums) {
79+
const n = parents.length;
80+
const result = Array.from({ length: n }, () => 1);
81+
const oneNode = nums.indexOf(1);
82+
83+
if (oneNode === -1) return result;
84+
85+
const tree = Array.from({ length: n }, () => []);
86+
const visited = new Set();
87+
let currentNode = oneNode;
88+
let minMiss = 1;
89+
let prevNode = -1;
90+
91+
for (let node = 1; node < n; node++) {
92+
const parent = parents[node];
93+
94+
tree[parent].push(node);
95+
}
96+
97+
const visitNode = node => {
98+
visited.add(nums[node]);
99+
100+
for (const neighbor of tree[node]) {
101+
visitNode(neighbor);
102+
}
103+
};
104+
105+
while (currentNode !== -1) {
106+
visited.add(nums[currentNode]);
107+
108+
for (const node of tree[currentNode]) {
109+
if (node === prevNode) continue;
110+
111+
visitNode(node);
112+
}
113+
114+
while (visited.has(minMiss)) {
115+
minMiss += 1;
116+
}
117+
118+
result[currentNode] = minMiss;
119+
prevNode = currentNode;
120+
currentNode = parents[currentNode];
121+
}
122+
123+
return result;
124+
};
125+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {number[]} parents
3+
* @param {number[]} nums
4+
* @return {number[]}
5+
*/
6+
const smallestMissingValueSubtree = function (parents, nums) {
7+
const n = parents.length;
8+
const result = Array.from({ length: n }, () => 1);
9+
const oneNode = nums.indexOf(1);
10+
11+
if (oneNode === -1) return result;
12+
13+
const tree = Array.from({ length: n }, () => []);
14+
const visited = new Set();
15+
let currentNode = oneNode;
16+
let minMiss = 1;
17+
let prevNode = -1;
18+
19+
for (let node = 1; node < n; node++) {
20+
const parent = parents[node];
21+
22+
tree[parent].push(node);
23+
}
24+
25+
const visitNode = node => {
26+
visited.add(nums[node]);
27+
28+
for (const neighbor of tree[node]) {
29+
visitNode(neighbor);
30+
}
31+
};
32+
33+
while (currentNode !== -1) {
34+
visited.add(nums[currentNode]);
35+
36+
for (const node of tree[currentNode]) {
37+
if (node === prevNode) continue;
38+
39+
visitNode(node);
40+
}
41+
42+
while (visited.has(minMiss)) {
43+
minMiss += 1;
44+
}
45+
46+
result[currentNode] = minMiss;
47+
prevNode = currentNode;
48+
currentNode = parents[currentNode];
49+
}
50+
51+
return result;
52+
};

0 commit comments

Comments
 (0)