The trick here is to name the state correctly, then let the implementation follow. For 690. Employee Importance, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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: straightforward implementation.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are getImportance.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- 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), where N is the number of employees. We might query each employee in dfs.
- Space: O(N), the size of the implicit call stack when evaluating dfs.
Guide
C++ Solution
Your submission
The accepted solution
01/**
02You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.
03
04For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.
05
06Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.
07
08Example 1:
09
10Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
11Output: 11
12Explanation:
13Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
14
15
16Note:
17
18One employee has at most one direct leader and may have several subordinates.
19The maximum number of employees won't exceed 2000.
20**/
21
22/**
23Complexity Analysis
24Time Complexity: O(N), where N is the number of employees. We might query each employee in dfs.
25Space Complexity: O(N), the size of the implicit call stack when evaluating dfs.
26**/
27
28/Runtime: 40 ms, faster than 65.96% of C++ online submissions for Employee Importance.
29//Memory Usage: 25.6 MB, less than 20.59% of C++ online submissions for Employee Importance.
30
31/*
32// Employee info
33class Employee {
34public:
35 // It's the unique ID of each node.
36 // unique id of this employee
37 int id;
38 // the importance value of this employee
39 int importance;
40 // the id of direct subordinates
41 vector<int> subordinates;
42};
43*/
44class Solution {
45public:
46 int getImportance(vector<Employee*> employees, int id) {
47 int ans = 0;
48 for(Employee* e : employees){
49 if(e->id==id){
50 ans = e->importance;
51 for(int s : e->subordinates){
52 ans += getImportance(employees, s);
53 }
54 return ans;
55 }
56 }
57 return 0;
58 }
59};
Cost