Async Batch Processing
JavaScript
Hard
3 views
Problem Description
Given n numbers and batch size b. Process each batch in parallel, but inside a batch do series (await each). Output total batches and the final sum.
Input Format
Line1: n b. Line2: n integers.
Output Format
Two values: batches sum.
Official Solution
const fs=require('fs');const raw=fs.readFileSync(0,'utf8').trim();if(!raw)process.exit(0);const a=raw.split(/\\s+/);let i=0;const n=Number(a[i++]);const b=Number(a[i++]);let arr=[];for(let j=0;j<n;j++)arr.push(BigInt(a[i++]));const step=async x=>{await Promise.resolve();return x;};(async()=>{let batches=0;let sum=0n;for(let s=0;s<n;s+=b){batches++;const chunk=arr.slice(s,s+b);await Promise.all(chunk.map(async v=>{let cur=0n;cur=await step(cur+v);sum+=cur;}));}process.stdout.write(batches+' '+sum.toString());})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!