Skip to content

Commit 725b7bc

Browse files
committed
Add solution #1376
1 parent c2850ec commit 725b7bc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,262 LeetCode solutions in JavaScript
1+
# 1,263 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1048,6 +1048,7 @@
10481048
1373|[Maximum Sum BST in Binary Tree](./solutions/1373-maximum-sum-bst-in-binary-tree.js)|Hard|
10491049
1374|[Generate a String With Characters That Have Odd Counts](./solutions/1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
10501050
1375|[Number of Times Binary String Is Prefix-Aligned](./solutions/1375-number-of-times-binary-string-is-prefix-aligned.js)|Medium|
1051+
1376|[Time Needed to Inform All Employees](./solutions/1376-time-needed-to-inform-all-employees.js)|Medium|
10511052
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
10521053
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
10531054
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 1376. Time Needed to Inform All Employees
3+
* https://leetcode.com/problems/time-needed-to-inform-all-employees/
4+
* Difficulty: Medium
5+
*
6+
* A company has n employees with a unique ID for each employee from 0 to n - 1. The head
7+
* of the company is the one with headID.
8+
*
9+
* Each employee has one direct manager given in the manager array where manager[i] is the
10+
* direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that
11+
* the subordination relationships have a tree structure.
12+
*
13+
* The head of the company wants to inform all the company employees of an urgent piece of
14+
* news. He will inform his direct subordinates, and they will inform their subordinates,
15+
* and so on until all employees know about the urgent news.
16+
*
17+
* The i-th employee needs informTime[i] minutes to inform all of his direct subordinates
18+
* (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
19+
*
20+
* Return the number of minutes needed to inform all the employees about the urgent news.
21+
*/
22+
23+
/**
24+
* @param {number} n
25+
* @param {number} headID
26+
* @param {number[]} manager
27+
* @param {number[]} informTime
28+
* @return {number}
29+
*/
30+
var numOfMinutes = function(n, headID, manager, informTime) {
31+
const adjacencyList = Array.from({ length: n }, () => []);
32+
33+
for (let i = 0; i < n; i++) {
34+
if (manager[i] !== -1) {
35+
adjacencyList[manager[i]].push(i);
36+
}
37+
}
38+
39+
return calculateTime(headID);
40+
41+
function calculateTime(employee) {
42+
let maxSubordinateTime = 0;
43+
44+
for (const subordinate of adjacencyList[employee]) {
45+
maxSubordinateTime = Math.max(maxSubordinateTime, calculateTime(subordinate));
46+
}
47+
48+
return informTime[employee] + maxSubordinateTime;
49+
}
50+
};

0 commit comments

Comments
 (0)