We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my code in java...Passed all the test cases.....
staticlong[]riddle(long[]arr){// complete the functionintn=arr.length;Stack<Integer>st=newStack<>();int[]left=newint[n+1];int[]right=newint[n+1];for(inti=0;i<n;i++){left[i]=-1;right[i]=n;}for(inti=0;i<n;i++){while(!st.isEmpty()&&arr[st.peek()]>=arr[i])st.pop();if(!st.isEmpty())left[i]=st.peek();st.push(i);}while(!st.isEmpty()){st.pop();}for(inti=n-1;i>=0;i--){while(!st.isEmpty()&&arr[st.peek()]>=arr[i])st.pop();if(!st.isEmpty())right[i]=st.peek();st.push(i);}longans[]=newlong[n+1];for(inti=0;i<=n;i++){ans[i]=0;}for(inti=0;i<n;i++){intlen=right[i]-left[i]-1;ans[len]=Math.max(ans[len],arr[i]);}for(inti=n-1;i>=1;i--){ans[i]=Math.max(ans[i],ans[i+1]);}long[]res=newlong[n];for(inti=1;i<=n;i++){res[i-1]=ans[i];}returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Min Max Riddle
You are viewing a single comment's thread. Return to all comments →
Here is my code in java...Passed all the test cases.....