Async Task Order by Priority (Simulation)
JavaScript
Hard
5 views
Problem Description
You have n tasks, each has priority and value. You always pick highest priority next (bigger is higher). Process in that order and print the final values (value*2).
Input Format
First line n. Next n lines: priority value.
Output Format
n lines of results in execution order.
Sample Test Case
Input:
4
2 10
5 1
5 3
1 7
Official Solution
const fs=require('fs');const lines=fs.readFileSync(0,'utf8').trim().split(/\
?\
/);if(!lines[0])process.exit(0);const n=Number(lines[0].trim());let tasks=[];for(let i=1;i<=n;i++){const [pr,val]=lines[i].trim().split(/\\s+/).map(Number);tasks.push([pr,val,i]);}tasks.sort((a,b)=>b[0]-a[0]||a[2]-b[2]);const run=async v=>{await Promise.resolve();return v*2;};(async()=>{let out=[];for(const t of tasks){out.push(String(await run(t[1])));}process.stdout.write(out.join('\
'));})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!