Created
May 20, 2020 07:02
-
-
Save blacksheep557/10cface4571f8bef507226ecf419e636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function taskScheduler(tasks = [], n = 1) { | |
| let charMap = {}; | |
| tasks.forEach((task) => { | |
| if (charMap[task] !== undefined) { | |
| charMap[task] += 1 | |
| } else { | |
| charMap[task] = 1; | |
| } | |
| }); | |
| let charList = []; | |
| for (let task in charMap) { | |
| charList.push([task, charMap[task]]) | |
| } | |
| charList.sort((a, b) => b[1] - a[1]); | |
| let maxFrequency = charList[0][1]; | |
| let idleSlots = (maxFrequency - 1) * n; | |
| for (let i = 1; i < charList.length; i++) { | |
| if (charList[i][1] === maxFrequency) { | |
| idleSlots -= maxFrequency - 1 | |
| } else { | |
| idleSlots -= charList[i][1] | |
| } | |
| } | |
| console.log(charList); | |
| return idleSlots <= 0 ? tasks.length : tasks.length + idleSlots; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment