-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0621-task-scheduler.js
More file actions
29 lines (25 loc) · 883 Bytes
/
0621-task-scheduler.js
File metadata and controls
29 lines (25 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Task Scheduler
* Time Complexity: O(T)
* Space Complexity: O(U)
*/
var leastInterval = function (tasks, n) {
const taskFrequencies = new Map();
let highestFrequencyValue = 0;
let highestFrequencyCount = 0;
for (const singleTask of tasks) {
const currentFrequency = (taskFrequencies.get(singleTask) || 0) + 1;
taskFrequencies.set(singleTask, currentFrequency);
if (currentFrequency > highestFrequencyValue) {
highestFrequencyValue = currentFrequency;
highestFrequencyCount = 1;
} else if (currentFrequency === highestFrequencyValue) {
highestFrequencyCount++;
}
}
const cyclesForMaxFrequency = highestFrequencyValue - 1;
const slotLengthPerCycle = n + 1;
const intervalsFromSchedule =
cyclesForMaxFrequency * slotLengthPerCycle + highestFrequencyCount;
return Math.max(tasks.length, intervalsFromSchedule);
};