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.
publicstaticinttwoStacks(intmaxSum,List<Integer>a,List<Integer>b){// Write your code hereStack<Integer>st1=newStack<>();Stack<Integer>st2=newStack<>();for(inti=0;i<a.size();i++){st1.push(a.get(i));}for(inti=0;i<b.size();i++){st2.push(b.get(i));}intlengthB=0;intsum=0;while(lengthB<b.size()&&(sum+b.get(lengthB))<=maxSum){sum+=b.get(lengthB);lengthB++;}intmaxScore=lengthB;for(inti=0;i<a.size();i++){sum+=a.get(i);while(sum>maxSum&&lengthB>0){lengthB--;sum-=b.get(lengthB);}if(sum>maxSum){break;}maxScore=Math.max(maxScore,lengthB+i+1);}returnmaxScore;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Two Stacks
You are viewing a single comment's thread. Return to all comments →
Here is my code in java...