leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1600.cpp (675B)


      1 class ThroneInheritance {
      2     unordered_map<string, vector<string>> children;
      3     unordered_set<string> dead;
      4 
      5     const string king;
      6 
      7   public:
      8     ThroneInheritance(const string &king) : king(king) {}
      9     void birth(const string &parent, const string &child) { children[parent].push_back(child); }
     10     void death(const string &name) { dead.insert(name); }
     11 
     12     vector<string> getInheritanceOrder() {
     13         order.clear();
     14         rec(king);
     15         return order;
     16     }
     17 
     18   private:
     19     vector<string> order;
     20     void rec(const string &name) {
     21         if (!dead.count(name)) order.push_back(name);
     22         for (const string &s : children[name])
     23             rec(s);
     24     }
     25 };