Skip to content

Commit 2e8da12

Browse files
solves throne inheritance in java
1 parent aa6caf7 commit 2e8da12

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@
583583
| 1588 | [Sum of All Odd Length Subarrays](https://leetcode.com/problems/sum-of-all-odd-length-subarrays) | [![Java](assets/java.png)](src/SumOfAllOddLengthSubArrays.java) | |
584584
| 1592 | [Rearrange Spaces Between Words](https://leetcode.com/problems/rearrange-spaces-between-words) | [![Java](assets/java.png)](src/RearrangeSpacesBetweenWords.java) | |
585585
| 1598 | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder) | [![Java](assets/java.png)](src/CrawlerLogFolder.java) | |
586+
| 1600 | [Throne Inheritance](https://leetcode.com/problems/throne-inheritance) | [![Java](assets/java.png)](src/ThroneInheritance.java) | |
586587
| 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system) | [![Java](assets/java.png)](src/DesignParkingSystem.java) | |
587588
| 1608 | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x) | [![Java](assets/java.png)](src/SpecialArrayWithXElementsGreaterThanEqualToX.java) | |
588589
| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses) | [![Java](assets/java.png)](src/MaximumNestingDepthOfTheParentheses.java) | |

Diff for: src/ThroneInheritance.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://leetcode.com/problems/throne-inheritance
2+
// N = people in royal family
3+
// birth() T: O(1)
4+
// death() T: O(1)
5+
// getInheritanceOrder T: O(N)
6+
// S: O(N)
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class ThroneInheritance {
14+
private static final class Person {
15+
private final String name;
16+
private boolean isAlive = true;
17+
private final List<Person> children = new ArrayList<>();
18+
19+
Person(String name) {
20+
this.name = name;
21+
}
22+
23+
void markDead() {
24+
this.isAlive = false;
25+
}
26+
27+
void addChild(Person child) {
28+
this.children.add(child);
29+
}
30+
}
31+
32+
private final Map<String, Person> royalFamily = new HashMap<>();
33+
private final Person king;
34+
35+
public ThroneInheritance(String kingName) {
36+
king = new Person(kingName);
37+
royalFamily.put(kingName, king);
38+
}
39+
40+
public void birth(String parentName, String childName) {
41+
Person child = new Person(childName);
42+
royalFamily.put(childName, child);
43+
royalFamily.get(parentName).addChild(child);
44+
}
45+
46+
public void death(String name) {
47+
royalFamily.get(name).markDead();
48+
}
49+
50+
public List<String> getInheritanceOrder() {
51+
final List<String> names = new ArrayList<>();
52+
getInheritanceOrder(king, names);
53+
return names;
54+
}
55+
56+
public void getInheritanceOrder(Person person, List<String> names) {
57+
if (person == null) return;
58+
if (person.isAlive) names.add(person.name);
59+
for (Person child : person.children) {
60+
getInheritanceOrder(child, names);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)