c++, 3 lines, clean code, time complexity O(n), space complexity O(1)


  • 0
    G

    We have a lot of solutions, one of it is by solve with recursive, our initial sum is equal to value of importance, then we add other importance values from subordinates easily, sorry for poor english :)

        int getImportance(vector<Employee*> employees, int id) {        
            int sum = employees[id - 1]->importance;
            for (auto newid : employees[id - 1]->subordinates)
                sum += getImportance(employees, newid);
            return sum;
        }
    

  • 0
    G

    Failed on the following test case:

    [[2,5,[]]]
    2


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.