Skip to content

Commit 37f1935

Browse files
add 1376
1 parent 77b1317 commit 37f1935

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1376.java) | |Medium||DFS
1112
|1375|[Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1375.java) | |Medium||Array
1213
|1374|[Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1374.java) | |Easy||String
1314
|1373|[Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1373.java) | |Hard||DP, BST
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
/**
11+
* 1376. Time Needed to Inform All Employees
12+
*
13+
* A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID.
14+
* Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it's guaranteed that the subordination relationships have a tree structure.
15+
* The head of the company wants to inform all the employees of the company of an urgent piece of news. He will inform his direct subordinates and they will inform their subordinates and so on until all employees know about the urgent news.
16+
* The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e After informTime[i] minutes, all his direct subordinates can start spreading the news).
17+
* Return the number of minutes needed to inform all the employees about the urgent news.
18+
*
19+
* Example 1:
20+
* Input: n = 1, headID = 0, manager = [-1], informTime = [0]
21+
* Output: 0
22+
* Explanation: The head of the company is the only employee in the company.
23+
*
24+
* Example 2:
25+
* Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
26+
* Output: 1
27+
* Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
28+
* The tree structure of the employees in the company is shown.
29+
*
30+
* Example 3:
31+
* Input: n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1]
32+
* Output: 21
33+
* Explanation: The head has id = 6. He will inform employee with id = 5 in 1 minute.
34+
* The employee with id = 5 will inform the employee with id = 4 in 2 minutes.
35+
* The employee with id = 4 will inform the employee with id = 3 in 3 minutes.
36+
* The employee with id = 3 will inform the employee with id = 2 in 4 minutes.
37+
* The employee with id = 2 will inform the employee with id = 1 in 5 minutes.
38+
* The employee with id = 1 will inform the employee with id = 0 in 6 minutes.
39+
* Needed time = 1 + 2 + 3 + 4 + 5 + 6 = 21.
40+
*
41+
* Example 4:
42+
* Input: n = 15, headID = 0, manager = [-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6], informTime = [1,1,1,1,1,1,1,0,0,0,0,0,0,0,0]
43+
* Output: 3
44+
* Explanation: The first minute the head will inform employees 1 and 2.
45+
* The second minute they will inform employees 3, 4, 5 and 6.
46+
* The third minute they will inform the rest of employees.
47+
*
48+
* Example 5:
49+
* Input: n = 4, headID = 2, manager = [3,3,-1,2], informTime = [0,0,162,914]
50+
* Output: 1076
51+
*
52+
* Constraints:
53+
* 1 <= n <= 10^5
54+
* 0 <= headID < n
55+
* manager.length == n
56+
* 0 <= manager[i] < n
57+
* manager[headID] == -1
58+
* informTime.length == n
59+
* 0 <= informTime[i] <= 1000
60+
* informTime[i] == 0 if employee i has no subordinates.
61+
* It is guaranteed that all the employees can be informed.
62+
* */
63+
public class _1376 {
64+
public static class Solution1 {
65+
int maxMinutes = 0;
66+
67+
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
68+
boolean[] visited = new boolean[manager.length];
69+
Set<Integer> managerIdSet = new HashSet<>();
70+
for (int id : manager) {
71+
managerIdSet.add(id);
72+
}
73+
Set<Integer> visitedEmployees = new HashSet<>();
74+
for (int i = 0; i < n; i++) {
75+
visitedEmployees.add(i);
76+
}
77+
78+
Map<Integer, List<Integer>> map = new HashMap<>();
79+
for (int i = 0; i < manager.length; i++) {
80+
if (!map.containsKey(manager[i])) {
81+
map.put(manager[i], new ArrayList<>());
82+
}
83+
map.get(manager[i]).add(i);
84+
}
85+
backtracking(visited, headID, 0, informTime, managerIdSet, visitedEmployees, map);
86+
return maxMinutes;
87+
}
88+
89+
private void backtracking(boolean[] visited, int managerId, int currentMinutes, int[] informTime, Set<Integer> managerIdSet, Set<Integer> visitedEmployees, Map<Integer, List<Integer>> map) {
90+
if (visitedEmployees.contains(managerId)) {
91+
visitedEmployees.remove(managerId);
92+
}
93+
if (!managerIdSet.contains(managerId)) {
94+
maxMinutes = Math.max(currentMinutes, maxMinutes);
95+
return;
96+
}
97+
if (visitedEmployees.isEmpty()) {
98+
return;
99+
}
100+
visited[managerId] = true;
101+
if (map.containsKey(managerId)) {
102+
List<Integer> suboridnates = map.get(managerId);
103+
for (int subordinate : suboridnates) {
104+
if (!visited[subordinate]) {
105+
backtracking(visited, subordinate, currentMinutes + informTime[managerId], informTime, managerIdSet, visitedEmployees, map);
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1376;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _1376Test {
9+
private static _1376.Solution1 solution1;
10+
private static int[] manager;
11+
private static int[] informTime;
12+
13+
@Test
14+
public void test1() {
15+
solution1 = new _1376.Solution1();
16+
manager = new int[]{-1};
17+
informTime = new int[]{0};
18+
assertEquals(0, solution1.numOfMinutes(1, 0, manager, informTime));
19+
}
20+
21+
@Test
22+
public void test2() {
23+
solution1 = new _1376.Solution1();
24+
manager = new int[]{2, 2, -1, 2, 2, 2};
25+
informTime = new int[]{0, 0, 1, 0, 0, 0};
26+
assertEquals(1, solution1.numOfMinutes(6, 2, manager, informTime));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
solution1 = new _1376.Solution1();
32+
manager = new int[]{1, 2, 3, 4, 5, 6, -1};
33+
informTime = new int[]{0, 6, 5, 4, 3, 2, 1};
34+
assertEquals(21, solution1.numOfMinutes(7, 6, manager, informTime));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
solution1 = new _1376.Solution1();
40+
manager = new int[]{-1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
41+
informTime = new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
42+
assertEquals(3, solution1.numOfMinutes(15, 0, manager, informTime));
43+
}
44+
45+
@Test
46+
public void test5() {
47+
solution1 = new _1376.Solution1();
48+
manager = new int[]{3, 3, -1, 2};
49+
informTime = new int[]{0, 0, 162, 914};
50+
assertEquals(1076, solution1.numOfMinutes(4, 2, manager, informTime));
51+
}
52+
53+
@Test
54+
public void test6() {
55+
solution1 = new _1376.Solution1();
56+
manager = new int[]{5, 9, 6, 10, -1, 8, 9, 1, 9, 3, 4};
57+
informTime = new int[]{0, 213, 0, 253, 686, 170, 975, 0, 261, 309, 337};
58+
assertEquals(2560, solution1.numOfMinutes(11, 4, manager, informTime));
59+
}
60+
61+
}

0 commit comments

Comments
 (0)