Skip to content

Instantly share code, notes, and snippets.

@silvareal
Last active January 6, 2025 15:31
Show Gist options
  • Select an option

  • Save silvareal/acc72c50ff47f4a3a3a36ec6d287c408 to your computer and use it in GitHub Desktop.

Select an option

Save silvareal/acc72c50ff47f4a3a3a36ec6d287c408 to your computer and use it in GitHub Desktop.
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