← Home

1600. Throne Inheritance

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
160

The trick here is to name the state correctly, then let the implementation follow. For 1600. Throne Inheritance, the solution in this repository is mainly a data structure design solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design, stack.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are birth, death, getInheritanceOrder.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 620 ms, faster than 98.00% of C++ online submissions for Throne Inheritance.
02//Memory Usage: 168.1 MB, less than 77.84% of C++ online submissions for Throne Inheritance.
03class Node{
04public:
05    string name;
06    bool dead;
07    vector<Node*> children;
08    
09    Node(){
10        name = "";
11        dead = false;
12    }
13    
14    Node(string n) : name(n) {
15        dead = false;
16    }
17};
18
19class ThroneInheritance {
20public:
21    Node* king;
22    unordered_map<string, Node*> name2node;
23    
24    ThroneInheritance(string kingName) {
25        king = new Node(kingName);
26        name2node[kingName] = king;
27    }
28    
29    void birth(string parentName, string childName) {
30        Node* child = new Node(childName);
31        name2node[parentName]->children.push_back(child);
32        name2node[childName] = child;
33    }
34    
35    void death(string name) {
36        name2node[name]->dead = true;
37    }
38    
39    vector<string> getInheritanceOrder() {
40        vector<string> ans;
41        
42        //dfs
43        stack<const Node*> stk;
44        
45        stk.push(king);
46        
47        while(!stk.empty()){
48            const Node* cur = stk.top(); stk.pop();
49            
50            if(!cur->dead){
51                ans.push_back(cur->name);
52            }
53            
54            //the older, the earlier popped
55            for(int i = cur->children.size()-1; i >= 0; --i){
56                stk.push(cur->children[i]);
57            }
58        }
59        
60        return ans;
61    }
62};
63
64/**
65 * Your ThroneInheritance object will be instantiated and called as such:
66 * ThroneInheritance* obj = new ThroneInheritance(kingName);
67 * obj->birth(parentName,childName);
68 * obj->death(name);
69 * vector<string> param_3 = obj->getInheritanceOrder();
70 */

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.