leetcode 1600. 皇位继承顺序(dfs)

题目

一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。

这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。

Successor(x, curOrder):如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:如果 x 是国王,那么返回 null否则,返回 Successor(x 的父亲, curOrder)否则,返回 x 不在 curOrder 中最年长的孩子
比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。

一开始, curOrder 为 [“king”].
调用 Successor(king, curOrder) ,返回 Alice ,所以我们将 Alice 放入 curOrder 中,得到 [“king”, “Alice”] 。
调用 Successor(Alice, curOrder) ,返回 Jack ,所以我们将 Jack 放入 curOrder 中,得到 [“king”, “Alice”, “Jack”] 。
调用 Successor(Jack, curOrder) ,返回 Bob ,所以我们将 Bob 放入 curOrder 中,得到 [“king”, “Alice”, “Jack”, “Bob”] 。
调用 Successor(Bob, curOrder) ,返回 null 。最终得到继承顺序为 [“king”, “Alice”, “Jack”, “Bob”] 。
通过以上的函数,我们总是能得到一个唯一的继承顺序。

请你实现 ThroneInheritance 类:

  • ThroneInheritance(string kingName) 初始化一个 ThroneInheritance 类的对象。国王的名字作为构造函数的参数传入。
  • void birth(string parentName, string childName) 表示 parentName 新拥有了一个名为 childName 的孩子。
  • void death(string name) 表示名为 name 的人死亡。一个人的死亡不会影响 Successor 函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。
  • string[] getInheritanceOrder() 返回 除去 死亡人员的当前继承顺序列表。

示例:

输入:
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
输出:
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]解释:
ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:king
t.birth("king", "andy"); // 继承顺序:king > andy
t.birth("king", "bob"); // 继承顺序:king > andy > bob
t.birth("king", "catherine"); // 继承顺序:king > andy > bob > catherine
t.birth("andy", "matthew"); // 继承顺序:king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // 继承顺序:king > andy > matthew > bob(已经去世)> alex > asha > catherine
t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]

提示:

  • 1 <= kingName.length, parentName.length, childName.length, name.length <= 15
  • kingName,parentName, childName 和 name 仅包含小写英文字母。
  • 所有的参数 childName 和 kingName 互不相同。
  • 所有 death 函数中的死亡名字 name 要么是国王,要么是已经出生了的人员名字。
  • 每次调用 birth(parentName, childName) 时,测试用例都保证 parentName 对应的人员是活着的。
  • 最多调用 105 次birth 和 death 。
  • 最多调用 10 次 getInheritanceOrder 。

image.png

解题思路

通过map和list组合,key记录人名,value记录孩子名字,这样就构造出一个具有层级结构的表示继承关系的多层树,根节点是国王,因为list的遍历次序就是插入次序,因此使用深度优先搜索遍历的顺序,就是继承关系。使用set维护去世的人名,避免加入到结果中去。

代码

    class ThroneInheritance {Map<String,List<String>> map=new HashMap<>();Set<String> death=new HashSet<>();String king;public ThroneInheritance(String kingName) {king=kingName;map.put(kingName,new ArrayList<>());}public void birth(String parentName, String childName) {if(!map.containsKey(parentName))map.put(parentName,new ArrayList<>());map.get(parentName).add(childName);}public void death(String name) {death.add(name);}public void dfs(List<String> res,String cur){if(!death.contains(cur)) res.add(cur);if(map.containsKey(cur)){for (String s : map.get(cur)) {dfs(res,s);}}}public List<String> getInheritanceOrder() {ArrayList<String> res = new ArrayList<>();dfs(res,king);return res;}}
/*** Your ThroneInheritance object will be instantiated and called as such:* ThroneInheritance obj = new ThroneInheritance(kingName);* obj.birth(parentName,childName);* obj.death(name);* List<String> param_3 = obj.getInheritanceOrder();*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390613.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vlookup match_INDEX-MATCH — VLOOKUP功能的升级

vlookup match电子表格/索引匹配 (SPREADSHEETS / INDEX-MATCH) In a previous article, we discussed about how and when to use VLOOKUP functions and what are the issues that we might face while using them. This article, on the other hand, will take you to a jou…

java基础-BigDecimal类常用方法介绍

java基础-BigDecimal类常用方法介绍 作者&#xff1a;尹正杰 版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的。原因是计算机二进制中&#xff0c;表示浮点数不精确造成的。这个时候…

节点对象转节点_节点流程对象说明

节点对象转节点The process object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process is one of them. It is an essential component in the …

PAT——1018. 锤子剪刀布

大家应该都会玩“锤子剪刀布”的游戏&#xff1a;两人同时给出手势&#xff0c;胜负规则如图所示&#xff1a; 现给出两人的交锋记录&#xff0c;请统计双方的胜、平、负次数&#xff0c;并且给出双方分别出什么手势的胜算最大。 输入格式&#xff1a; 输入第1行给出正整数N&am…

leetcode 1239. 串联字符串的最大长度

题目 二进制手表顶部有 4 个 LED 代表 小时&#xff08;0-11&#xff09;&#xff0c;底部的 6 个 LED 代表 分钟&#xff08;0-59&#xff09;。每个 LED 代表一个 0 或 1&#xff0c;最低位在右侧。 例如&#xff0c;下面的二进制手表读取 “3:25” 。 &#xff08;图源&am…

flask redis_在Flask应用程序中将Redis队列用于异步任务

flask redisBy: Content by Edward Krueger and Josh Farmer, and Douglas Franklin.作者&#xff1a; 爱德华克鲁格 ( Edward Krueger) 和 乔什法默 ( Josh Farmer )以及 道格拉斯富兰克林 ( Douglas Franklin)的内容 。 When building an application that performs time-co…

CentOS7下分布式文件系统FastDFS的安装 配置 (单节点)

背景 FastDFS是一个开源的轻量级分布式文件系统&#xff0c;为互联网量身定制&#xff0c;充分考虑了冗余备份、负载均衡、线性扩容等机制&#xff0c;并注重高可用、高性能等指标&#xff0c;解决了大容量存储和负载均衡的问题&#xff0c;特别适合以文件为载体的在线服务&…

如何修复会话固定漏洞_PHP安全漏洞:会话劫持,跨站点脚本,SQL注入以及如何修复它们...

如何修复会话固定漏洞PHP中的安全性 (Security in PHP) When writing PHP code it is very important to keep the following security vulnerabilities in mind to avoid writing insecure code.在编写PHP代码时&#xff0c;记住以下安全漏洞非常重要&#xff0c;以避免编写不…

剑指 Offer 38. 字符串的排列

题目 输入一个字符串&#xff0c;打印出该字符串中字符的所有排列。 你可以以任意顺序返回这个字符串数组&#xff0c;但里面不能有重复元素。 示例: 输入&#xff1a;s “abc” 输出&#xff1a;[“abc”,“acb”,“bac”,“bca”,“cab”,“cba”] 限制&#xff1a; 1…

前馈神经网络中的前馈_前馈神经网络在基于趋势的交易中的有效性(1)

前馈神经网络中的前馈This is a preliminary showcase of a collaborative research by Seouk Jun Kim (Daniel) and Sunmin Lee. You can find our contacts at the bottom of the article.这是 Seouk Jun Kim(Daniel) 和 Sunmin Lee 进行合作研究的初步展示 。 您可以在文章底…

解释什么是快速排序算法?_解释排序算法

解释什么是快速排序算法?Sorting algorithms are a set of instructions that take an array or list as an input and arrange the items into a particular order.排序算法是一组指令&#xff0c;这些指令采用数组或列表作为输入并将项目按特定顺序排列。 Sorts are most c…

SpringBoot自动化配置的注解开关原理

我们以一个最简单的例子来完成这个需求&#xff1a;定义一个注解EnableContentService&#xff0c;使用了这个注解的程序会自动注入ContentService这个bean。 Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) Import(ContentConfiguration.class) public interfa…

hadoop将消亡_数据科学家:适应还是消亡!

hadoop将消亡Harvard Business Review marked the boom of Data Scientists in their famous 2012 article “Data Scientist: Sexiest Job”, followed by untenable demand in the past decade. [3]《哈佛商业评论 》在2012年著名的文章“数据科学家&#xff1a;最性感的工作…

剑指 Offer 15. 二进制中1的个数 and leetcode 1905. 统计子岛屿

题目 请实现一个函数&#xff0c;输入一个整数&#xff08;以二进制串形式&#xff09;&#xff0c;输出该数二进制表示中 1 的个数。例如&#xff0c;把 9 表示成二进制是 1001&#xff0c;有 2 位是 1。因此&#xff0c;如果输入 9&#xff0c;则该函数输出 2。 示例 1&…

[转]kafka介绍

转自 https://www.cnblogs.com/hei12138/p/7805475.html kafka介绍1.1. 主要功能 根据官网的介绍&#xff0c;ApacheKafka是一个分布式流媒体平台&#xff0c;它主要有3种功能&#xff1a; 1&#xff1a;It lets you publish and subscribe to streams of records.发布和订阅消…

如何开始android开发_如何开始进行Android开发

如何开始android开发Android开发简介 (An intro to Android Development) Android apps can be a great, fun way to get into the world of programming. Officially programmers can use Java, Kotlin, or C to develop for Android. Though there may be API restrictions, …

httpd2.2的配置文件常见设置

目录 1、启动报错&#xff1a;提示没有名字fqdn2、显示服务器版本信息3、修改监听的IP和Port3、持久连接4 、MPM&#xff08; Multi-Processing Module &#xff09;多路处理模块5 、DSO&#xff1a;Dynamic Shared Object6 、定义Main server &#xff08;主站点&#xff09; …

leetcode 149. 直线上最多的点数

题目 给你一个数组 points &#xff0c;其中 points[i] [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。 示例 1&#xff1a; 输入&#xff1a;points [[1,1],[2,2],[3,3]] 输出&#xff1a;3 示例 2&#xff1a; 输入&#xff1a;points [[1,1],[3,…

solidity开发以太坊代币智能合约

智能合约开发是以太坊编程的核心之一&#xff0c;而代币是区块链应用的关键环节&#xff0c;下面我们来用solidity语言开发一个代币合约的实例&#xff0c;希望对大家有帮助。 以太坊的应用被称为去中心化应用&#xff08;DApp&#xff09;&#xff0c;DApp的开发主要包括两大部…

2019大数据课程_根据数据,2019年最佳免费在线课程

2019大数据课程As we do each year, Class Central has tallied the best courses of the previous year, based on thousands of learner reviews. (Here are the rankings from 2015, 2016, 2017, and 2018.) 与我们每年一样&#xff0c;根据数千名学习者的评论&#xff0c; …