Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b75751d

Browse files
Task Scheduler
1 parent fd0478c commit b75751d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎0621_taskScheduler.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string[]} taks List of tasks A-Z.
3+
* @param {number} n Cooling time between running same task.
4+
* @return {number} Time CPU will take to finish all tasks.
5+
* @summary Task Scheduler {@link https://leetcode.com/problems/task-scheduler/submissions/}
6+
* @description Given array of CPU taks and minimum cooling period between same task, return the minimum units of time CPU will take to finish all tasks.
7+
* Space O(1) - One hash of frequencies up to 26 keys.
8+
* Time O(n) - N is number of tasks to execute (<26).
9+
*/
10+
const leastInterval = (tasks, n) => {
11+
let maxCount = 0;
12+
13+
const freq = tasks.reduce((acc, val) => {
14+
acc[val] ? acc[val]++ : (acc[val] = 1);
15+
if (acc[val] > maxCount) maxCount = acc[val];
16+
return acc;
17+
}, {});
18+
19+
const maxTasks = Object.keys(freq).reduce((acc, val) => (freq[val] === maxCount ? acc + 1 : acc), 0);
20+
21+
return Math.max(tasks.length, (maxCount - 1) * (n + 1) + maxTasks);
22+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /