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.pswp.cn/news/390613.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390613.shtml
英文地址,請注明出處:http://en.pswp.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; …