You are viewing a single comment's thread. Return to all comments →
Clean Java Accepted Solution
static int twoStacks(int x, int[] a, int[] b) { int localSum = 0; int localScore = 0; int indexA = 0; int indexB = 0; while(indexA < a.length && x >= localSum + a[indexA]){ localSum += a[indexA++]; localScore++; } while(indexB < b.length && x >= localSum + b[indexB]){ localSum += b[indexB++]; localScore++; } if(indexB == b.length) return localScore; int maxScore = localScore; for(; indexA > 0; indexA--){ if(indexB == b.length) break; localSum -= a[indexA-1]; localScore--; while(indexB < b.length && localSum + b[indexB] <= x){ localSum += b[indexB++]; localScore++; } maxScore = Math.max(localScore, maxScore); } return maxScore; }
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 →
Clean Java Accepted Solution