Guest User

Untitled

a guest
Sep 8th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.     static int twoStack(int x, int[] a, int[] b) {
  2.         int sum = 0, maxBucket=0;
  3.         int bj = 0, ai = 0;
  4.        
  5.         while(ai < a.length  && (sum+a[ai]) <= x ){
  6.             sum+=a[ai++];
  7.             maxBucket++; // add max buckets from 1st stack , sum <=x
  8.         }
  9.        
  10.         while(bj < b.length){ // add each element from b stack
  11.             sum+=b[bj++];
  12.            
  13.             while(sum>x && ai > 0){// while sum is larger
  14.                 sum-=a[--ai]; // keep removing element from a stack from top
  15.             }
  16.            
  17.             if(sum<=x && ai+bj>maxBucket){// if ai+aj > maxBucket
  18.                 maxBucket= ai+bj; // maxbucket is ai+aj
  19.             }
  20.         }
  21.        
  22.         return maxBucket;
  23.     }
Add Comment
Please, Sign In to add comment