Limit Concurrency with Error Count
JavaScript
Hard
3 views
Problem Description
Given n numbers and limit k. Task rejects if number is negative, else resolves with abs value. Run with max k concurrency and print okCount failCount.
Input Format
Line1: n k. Line2: n integers.
Output Format
Two integers ok fail.
Sample Test Case
Input:
7 3
-1 2 -3 4 0 -5 6
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/).map(Number);let i=0;const n=a[i++];const k=a[i++];let arr=a.slice(i,i+n);const task=async x=>{await Promise.resolve();if(x<0)throw new Error('NEG');return Math.abs(x);};(async()=>{let next=0,active=0,done=0;let ok=0,fail=0;await new Promise(resolve=>{const launch=()=>{while(active<k && next<n){const x=arr[next++];active++;task(x).then(()=>ok++).catch(()=>fail++).finally(()=>{active--;done++;if(done===n)resolve();else launch();});}};launch();});process.stdout.write(ok+' '+fail);})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!