Title: #1882 - Process Tasks Using Servers - LeetCode JavaScript Solutions
Open Graph Title: #1882 - Process Tasks Using Servers - LeetCode JavaScript Solutions
X Title: #1882 - Process Tasks Using Servers - LeetCode JavaScript Solutions
Description: You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the ith server, an...
Open Graph Description: You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the ith server, an...
X Description: You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the ith server, an...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/process-tasks-using-servers
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/process-tasks-using-servers","name":"#1882 - Process Tasks Using Servers - LeetCode JavaScript Solutions","description":"You are given two 0-indexed integer arrays servers and tasks of lengths n and m respectively. servers[i] is the weight of the ith server, an...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} servers\n * @param {number[]} tasks\n * @return {number[]}\n */\nvar assignTasks = function(servers, tasks) {\n const serverHeap = new PriorityQueue((a, b) => a[0] * 1000000 + a[1] - (b[0] * 1000000 + b[1]));\n const busyHeap = new PriorityQueue((a, b) => a[0] - b[0]);\n const result = new Array(tasks.length);\n\n for (let i = 0; i < servers.length; i++) {\n serverHeap.enqueue([servers[i], i]);\n }\n\n let time = 0;\n let taskIndex = 0;\n\n while (taskIndex < tasks.length) {\n time = Math.max(time, taskIndex);\n\n while (!busyHeap.isEmpty() && busyHeap.front()[0] <= time) {\n const [, weight, index] = busyHeap.dequeue();\n serverHeap.enqueue([weight, index]);\n }\n\n while (serverHeap.size() && taskIndex < tasks.length && taskIndex <= time) {\n const [weight, index] = serverHeap.dequeue();\n result[taskIndex] = index;\n busyHeap.enqueue([time + tasks[taskIndex], weight, index]);\n taskIndex++;\n }\n\n if (serverHeap.isEmpty() && taskIndex < tasks.length) {\n const [freeTime, weight, index] = busyHeap.dequeue();\n time = freeTime;\n serverHeap.enqueue([weight, index]);\n }\n }\n\n return result;\n};","keywords":"LeetCode 1882, #1882 - Process Tasks Using Servers, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/process-tasks-using-servers |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/process-tasks-using-servers/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1882-process-tasks-using-servers.js |
| Array | https://leetcodejavascript.com/tags/array |
| Heap Priority Queue | https://leetcodejavascript.com/tags/heap-priority-queue |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow