← Home

1376. Time Needed to Inform All Employees

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
stackC++Markdown
137

The trick here is to name the state correctly, then let the implementation follow. For 1376. Time Needed to Inform All Employees, the solution in this repository is mainly a stack 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: stack.

The notes already sitting in the source point us in the right direction:

  • DFS, recursive

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are dfs, numOfMinutes.

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//DFS, recursive
02//Runtime: 652 ms, faster than 24.40% of C++ online submissions for Time Needed to Inform All Employees.
03//Memory Usage: 86 MB, less than 100.00% of C++ online submissions for Time Needed to Inform All Employees.
04class Solution {
05public:
06    unordered_map<int, vector<int>> children;
07    vector<int> informTime;
08    int totalTime = 0;
09    
10    void dfs(int node, int curTime){
11        totalTime = max(totalTime, curTime);
12        for(int child : children[node]){
13            dfs(child, curTime + informTime[node]);
14        }
15    }
16    
17    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
18        for(int i = 0; i < manager.size(); i++){
19            //i's manager is manager[i]
20            children[manager[i]].push_back(i);
21        }
22        
23        this->informTime = informTime;
24        
25        dfs(headID, 0);
26        
27        return totalTime;
28    }
29};
30
31//DFS, iterative
32//https://leetcode.com/problems/time-needed-to-inform-all-employees/discuss/532680/Python3-dfs-recursively-and-iteratively
33//Runtime: 680 ms, faster than 21.86% of C++ online submissions for Time Needed to Inform All Employees.
34//Memory Usage: 88.4 MB, less than 100.00% of C++ online submissions for Time Needed to Inform All Employees.
35class Solution {
36public:
37    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
38        unordered_map<int, vector<int>> children;
39        
40        for(int i = 0; i < n; i++){
41            //don't need to record root's parent
42            // if(i == headID) continue;
43            children[manager[i]].push_back(i);
44        }
45        
46        stack<pair<int, int>> stk;
47        int ans = 0;
48        
49        stk.push(make_pair(headID, 0));
50        
51        while(!stk.empty()){
52            pair<int,int> p = stk.top(); stk.pop();
53            int node = p.first, curTime = p.second;
54            
55            // cout << node << " " << curTime << endl;
56            
57            ans = max(ans, curTime);
58            
59            for(int child : children[node]){
60                stk.push(make_pair(child, curTime+informTime[node]));
61            }
62        }
63        
64        return ans;
65    }
66};

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.