Last active
January 6, 2025 15:31
-
-
Save silvareal/acc72c50ff47f4a3a3a36ec6d287c408 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
| const tasks = ["A", "A", "A", "B", "B", "C"]; | |
| const n = 2; | |
| function minRestockingTimeWithPath(items, n) { | |
| const taskCounts = {}; | |
| for (const task of items) { | |
| taskCounts[task] = (taskCounts[task] || 0) + 1; | |
| } | |
| const taskQueue = Object.entries(taskCounts) | |
| .map(([task, count]) => ({ task, count })) | |
| .sort((a, b) => b.count - a.count); | |
| let time = 0; | |
| const cooldown = new Map(); | |
| const schedule = []; | |
| while (taskQueue.length > 0 || cooldown.size > 0) { | |
| time++; | |
| if (cooldown.has(time)) { | |
| const task = cooldown.get(time); | |
| cooldown.delete(time); | |
| taskQueue.push(task); | |
| } | |
| taskQueue.sort((a, b) => b.count - a.count); | |
| if (taskQueue.length > 0) { | |
| const currentTask = taskQueue.shift(); | |
| schedule.push(currentTask.task); | |
| currentTask.count--; | |
| if (currentTask.count > 0) { | |
| cooldown.set(time + n + 1, currentTask); | |
| } | |
| } else { | |
| schedule.push("idle"); | |
| } | |
| } | |
| return { | |
| time, | |
| schedule, | |
| }; | |
| } | |
| console.log(minRestockingTimeWithPath(items1, n1)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment