There was an error while loading. Please reload this page.
1 parent ed67885 commit 08882a8Copy full SHA for 08882a8
task-scheduler.js
@@ -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