Maximum of Minimums
JavaScript
Hard
5 views
Problem Description
For every window size from 1 to n, print the maximum among all minimums of windows of that size.
Input Format
One line: n then n integers.
Output Format
One line: n integers.
Sample Test Case
Input:
7 10 20 30 50 10 70 30
Output:
70 30 20 10 10 10 10
Official Solution
const fs=require('fs');const p=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!p[0])process.exit(0);let i=0;const n=Number(p[i++]);let a=new Array(n);for(let j=0;j<n;j++)a[j]=Number(p[i++]);let left=new Array(n),right=new Array(n);let st=[];for(let j=0;j<n;j++){while(st.length && a[st[st.length-1]]>=a[j])st.pop();left[j]=st.length?st[st.length-1]:-1;st.push(j);}st=[];for(let j=n-1;j>=0;j--){while(st.length && a[st[st.length-1]]>=a[j])st.pop();right[j]=st.length?st[st.length-1]:n;st.push(j);}let ans=new Array(n+1).fill(-Infinity);for(let j=0;j<n;j++){const len=right[j]-left[j]-1;ans[len]=Math.max(ans[len],a[j]);}for(let len=n-1;len>=1;len--)ans[len]=Math.max(ans[len],ans[len+1]);let out=[];for(let len=1;len<=n;len++)out.push(String(ans[len]));process.stdout.write(out.join(' '));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!