当前位置:网站首页>2003. 每棵子树内缺失的最小基因值 DFS
2003. 每棵子树内缺失的最小基因值 DFS
2022-08-04 03:38:00 【钰娘娘】
2003. 每棵子树内缺失的最小基因值
有一棵根节点为
0的 家族树 ,总共包含n个节点,节点编号为0到n - 1。给你一个下标从 0 开始的整数数组parents,其中parents[i]是节点i的父节点。由于节点0是 根 ,所以parents[0] == -1。总共有
105个基因值,每个基因值都用 闭区间[1, 105]中的一个整数表示。给你一个下标从 0 开始的整数数组nums,其中nums[i]是节点i的基因值,且基因值 互不相同 。请你返回一个数组
ans,长度为n,其中ans[i]是以节点i为根的子树内 缺失 的 最小 基因值。节点
x为根的 子树 包含节点x和它所有的 后代 节点。示例 1:
输入:parents = [-1,0,0,2], nums = [1,2,3,4] 输出:[5,1,1,1] 解释:每个子树答案计算结果如下: - 0:子树包含节点 [0,1,2,3] ,基因值分别为 [1,2,3,4] 。5 是缺失的最小基因值。 - 1:子树只包含节点 1 ,基因值为 2 。1 是缺失的最小基因值。 - 2:子树包含节点 [2,3] ,基因值分别为 [3,4] 。1 是缺失的最小基因值。 - 3:子树只包含节点 3 ,基因值为 4 。1是缺失的最小基因值。示例 2:
输入:parents = [-1,0,1,0,3,3], nums = [5,4,6,2,1,3] 输出:[7,1,1,4,2,1] 解释:每个子树答案计算结果如下: - 0:子树内包含节点 [0,1,2,3,4,5] ,基因值分别为 [5,4,6,2,1,3] 。7 是缺失的最小基因值。 - 1:子树内包含节点 [1,2] ,基因值分别为 [4,6] 。 1 是缺失的最小基因值。 - 2:子树内只包含节点 2 ,基因值为 6 。1 是缺失的最小基因值。 - 3:子树内包含节点 [3,4,5] ,基因值分别为 [2,1,3] 。4 是缺失的最小基因值。 - 4:子树内只包含节点 4 ,基因值为 1 。2 是缺失的最小基因值。 - 5:子树内只包含节点 5 ,基因值为 3 。1 是缺失的最小基因值。示例 3:
输入:parents = [-1,2,3,0,2,4,1], nums = [2,3,4,5,6,7,8] 输出:[1,1,1,1,1,1,1] 解释:所有子树都缺失基因值 1 。提示:
n == parents.length == nums.length2 <= n <= 1e5- 对于
i != 0,满足0 <= parents[i] <= n - 1parents[0] == -1parents表示一棵合法的树。1 <= nums[i] <= 1e5nums[i]互不相同。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/smallest-missing-genetic-value-in-each-subtree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
失败。这题dfs好想,但是想到有1的点才处理比较难
方法:dfs
1. 1如果存在只有1个
2. 如果不存在1那全填1
3. 如果存在1,那1到根只能占用树的至多一条路径,则不经1的点全都填1
4. 唯一含有1路径的路线dfs填充移动
class Solution {
int[] ans;
boolean[] hasOne;
int one;
public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {
int n = parents.length;
Map<Integer,List<Integer>> map = new HashMap<>();
for(int i = 1; i < n; i++){
map.putIfAbsent(parents[i],new ArrayList<>());
map.get(parents[i]).add(i);
}
hasOne = new boolean[n];
int pos = -1;
for(int i = 0; i < n; i++){
if(nums[i]==1){
pos = i;
break;
}
}
ans = new int[n];
if(pos == -1){
Arrays.fill(ans,1);
return ans;
}
one = pos;
hasOne[pos]=true;
while (pos!=0){
pos = parents[pos];
hasOne[pos]=true;
}
for(int i =0; i < n; i++){
if(!hasOne[i]) ans[i]=1;
}
fill(map,nums,one,parents);
return ans;
}
Set<Integer> visited = new HashSet<>();
int id = 1;
private void fill(Map<Integer, List<Integer>> map, int[] nums, int index,int[] parents) {
if(index==-1) return;
if(map.containsKey(index)){
add(map,nums,index);
}else{
visited.add(nums[index]);
}
for(;id<=nums.length+1;id++){
if(!visited.contains(id)){
ans[index]=id;
break;
}
}
fill(map,nums,parents[index],parents);
}
private void add(Map<Integer,List<Integer>> map, int[] nums, int index){
visited.add(nums[index]);
if(!map.containsKey(index)){
return;
}
for(Integer next:map.get(index)){
if(hasOne[next]) continue;
add(map,nums,next);
}
}
}边栏推荐
- 马尔可夫链
- 千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
- Returns the maximum number of palindromes in a string
- tkmapper的crud示例:
- 函数,递归以及dom简单操作
- LeetCode每日一题(2285. Maximum Total Importance of Roads)
- 全网没有之一的JMeter 接口测试流程详解
- 汇编语言之栈
- KingbaseES数据库启动失败,报“内存段超过可用内存”
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
猜你喜欢
![[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this](/img/d0/6ac51d0d51c907ed0e1578e038fffd.jpg)
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this

软件测试如何系统规划学习呢?

sqoop ETL tool

2022杭电多校联赛第五场 题解

6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch

说说数据治理中常见的20个问题
sql注入一般流程(附例题)

Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch

学会iframe并用其解决跨域问题

逻辑漏洞----其他类型
随机推荐
[Playwright Test Tutorial] 5 minutes to get started
STM8S project creation (STVD creation) --- use COSMIC to create a C language project
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
RSS订阅微信公众号初探-feed43
MySQL Query Exercise (1)
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
汇编语言之栈
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
sql语句查询String类型字段小于10的怎么查
How to systematically plan and learn software testing?
Based on the statistical QDirStat Qt directory
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
基于 SSE 实现服务端消息主动推送解决方案
说说数据治理中常见的20个问题
Asynchronous programming solution Generator generator function, iterator iterator, async/await, Promise
tkmapper的crud示例:
Exclude_reserved_words 排除关键字

