1376. Time Needed to Inform All Employees

Link

Solution

Build graph, and traversal to find the max time of each layer

 public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
        List<Integer>[] graph = new List[n];
        for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
        for(int i = 0; i < n; i++){
            if(manager[i] == -1) continue;
            graph[manager[i]].add(i);
        }
        
        return dfs(graph, headID, informTime);
    }
    
    public int dfs(List<Integer>[] graph, int id, int[] informTime){
        int ret = informTime[id];
        int max= 0;
        for(int member : graph[id]){
            max = Math.max(max, dfs(graph, member, informTime));
        }
        return ret + max;
    }

Last updated

Was this helpful?