Skip to content

Commit 08882a8

Browse files
committed
Task Schedular 🥰
1 parent ed67885 commit 08882a8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

task-scheduler.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {character[]} tasks
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
const leastInterval = (tasks, n) => {
7+
8+
let max = 0;
9+
let maxCount = 0;
10+
11+
const map = {};
12+
for (let task of tasks) {
13+
map[task] = ~~map[task] + 1;
14+
if (map[task] === max) {
15+
maxCount++;
16+
} else if (map[task] > max) {
17+
max = map[task];
18+
maxCount = 1;
19+
}
20+
}
21+
22+
const groups = max - 1;
23+
const coolingLeft = n - (maxCount - 1);
24+
25+
26+
const emptySlots = groups * coolingLeft;
27+
const tasksLeft = tasks.length - max * maxCount;
28+
29+
30+
const idle = Math.max(0, emptySlots - tasksLeft);
31+
32+
return tasks.length + idle;
33+
};

0 commit comments

Comments
 (0)