Run Until First Success (Parallel)
JavaScript
Hard
3 views
Problem Description
Given n values. For each value x, async task succeeds if x%3==0 and returns x. Run all tasks in parallel and print the smallest successful value, else print NONE.
Input Format
Line1: n. Line2: n integers.
Output Format
Value or NONE.
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 arr=a.slice(i,i+n);const task=async x=>{await Promise.resolve();if(x%3!==0)throw new Error('NO');return x;};(async()=>{const r=await Promise.allSettled(arr.map(task));let best=null;for(const it of r){if(it.status==='fulfilled'){const v=it.value;if(best===null||v<best)best=v;}}process.stdout.write(best===null?'NONE':String(best));})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!