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

Commit 5865203

Browse files
committed
Add solution #2003
1 parent 6731205 commit 5865203

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,681 LeetCode solutions in JavaScript
1+
# 1,682 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1537,6 +1537,7 @@
15371537
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15381538
2001|[Number of Pairs of Interchangeable Rectangles](./solutions/2001-number-of-pairs-of-interchangeable-rectangles.js)|Medium|
15391539
2002|[Maximum Product of the Length of Two Palindromic Subsequences](./solutions/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js)|Medium|
1540+
2003|[Smallest Missing Genetic Value in Each Subtree](./solutions/2003-smallest-missing-genetic-value-in-each-subtree.js)|Hard|
15401541
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15411542
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15421543
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 2003. Smallest Missing Genetic Value in Each Subtree
3+
* https://leetcode.com/problems/smallest-missing-genetic-value-in-each-subtree/
4+
* Difficulty: Hard
5+
*
6+
* There is a family tree rooted at 0 consisting of n nodes numbered 0 to n - 1. You are given a
7+
* 0-indexed integer array parents, where parents[i] is the parent for node i. Since node 0 is
8+
* the root, parents[0] == -1.
9+
*
10+
* There are 105 genetic values, each represented by an integer in the inclusive range [1, 105].
11+
* You are given a 0-indexed integer array nums, where nums[i] is a distinct genetic value for
12+
* node i.
13+
*
14+
* Return an array ans of length n where ans[i] is the smallest genetic value that is missing
15+
* from the subtree rooted at node i.
16+
*
17+
* The subtree rooted at a node x contains node x and all of its descendant nodes.
18+
*/
19+
20+
/**
21+
* @param {number[]} parents
22+
* @param {number[]} nums
23+
* @return {number[]}
24+
*/
25+
var smallestMissingValueSubtree = function(parents, nums) {
26+
const n = parents.length;
27+
const result = new Array(n).fill(1);
28+
const children = Array.from({ length: n }, () => []);
29+
const seen = new Set();
30+
let maxMissing = 1;
31+
32+
for (let i = 1; i < n; i++) {
33+
children[parents[i]].push(i);
34+
}
35+
36+
const nodeWithOne = nums.indexOf(1);
37+
if (nodeWithOne === -1) return result;
38+
39+
let current = nodeWithOne;
40+
while (current !== -1) {
41+
const stack = [current];
42+
while (stack.length) {
43+
const node = stack.pop();
44+
seen.add(nums[node]);
45+
for (const child of children[node]) {
46+
if (!seen.has(nums[child])) stack.push(child);
47+
}
48+
}
49+
while (seen.has(maxMissing)) maxMissing++;
50+
result[current] = maxMissing;
51+
current = parents[current];
52+
}
53+
54+
return result;
55+
};

0 commit comments

Comments
 (0)