Last active
May 25, 2020 05:33
-
-
Save blacksheep557/1fa88888187d4edad208e1da7c48007e 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 scheduler(array = [], n = 2) { | |
| let taskCount = new Map(); | |
| array.forEach((char) => { | |
| if (taskCount.get(char) !== undefined) { | |
| taskCount.set(char, taskCount.get(char) + 1) | |
| } else { | |
| taskCount.set(char, 1) | |
| } | |
| }); | |
| let sortedFrequencies = [...taskCount.entries()].sort((a, b) => b[0] - a[0]); | |
| let scheduledTasks = []; | |
| let counter = 0; | |
| while (taskCount.size !== 0) { | |
| let currentLength = scheduledTasks.length; | |
| for (let pointer = 0; pointer < sortedFrequencies.length; pointer++) { | |
| if (canBeInserted(scheduledTasks, sortedFrequencies[pointer][0], n)) { | |
| scheduledTasks.push(sortedFrequencies[pointer][0]); | |
| taskCount.set(sortedFrequencies[pointer][0], sortedFrequencies[pointer][1] - 1); | |
| if (sortedFrequencies[pointer][1] - 1 === 0) { | |
| taskCount.delete(sortedFrequencies[pointer][0]) | |
| } | |
| break; | |
| } | |
| } | |
| if (currentLength === scheduledTasks.length) { | |
| scheduledTasks.push(null); | |
| } | |
| counter++; | |
| if (scheduledTasks.length === n + 1) scheduledTasks.shift(); | |
| sortedFrequencies = [...taskCount.entries()].sort((a, b) => b[1] - a[1]); | |
| } | |
| return counter; | |
| } | |
| function canBeInserted(list = [], element = '', k = 2) { | |
| return list.indexOf(element) === -1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment