• + 2 comments

    Here is my code in java...

    public static int twoStacks(int maxSum, List<Integer> a, List<Integer> b) {
        // Write your code here
        Stack<Integer> st1 = new Stack<>();
        Stack<Integer> st2 = new Stack<>();
        for(int i=0; i<a.size(); i++){
          st1.push(a.get(i));
        }
        for(int i=0; i<b.size(); i++){
          st2.push(b.get(i));
        }
        int lengthB=0;
        int sum = 0;
        while(lengthB<b.size() && (sum+b.get(lengthB)) <= maxSum ){
          sum += b.get(lengthB);
          lengthB++;
        }
        int maxScore = lengthB;
        for(int i=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 );
        }
        return maxScore;
    
        }