Sort by

recency

|

408 Discussions

|

  • + 0 comments
    import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int g = sc.nextInt();
            for (int tc = 0; tc < g; tc++) {
                int n = sc.nextInt();
                int m = sc.nextInt();
                int x = sc.nextInt();
                int[] a = readArray(sc, n);
                int[] b = readArray(sc, m);
    
                System.out.println(solve(a, b, x));
            }
    
            sc.close();
        }
    
        static int[] readArray(Scanner sc, int size) {
            int[] result = new int[size];
            for (int i = 0; i < result.length; i++) {
                result[i] = sc.nextInt();
            }
            return result;
        }
    
        static int solve(int[] a, int[] b, int x) {
            int lengthB = 0;
            int sum = 0;
            while (lengthB < b.length && sum + b[lengthB] <= x) {
                sum += b[lengthB];
                lengthB++;
            }
    
            int maxScore = lengthB;
            for (int lengthA = 1; lengthA <= a.length; lengthA++) {
                sum += a[lengthA - 1];
    
                while (sum > x && lengthB > 0) {
                    lengthB--;
                    sum -= b[lengthB];
                }
    
                if (sum > x) {
                    break;
                }
    
                maxScore = Math.max(maxScore, lengthA + lengthB);
            }
            return maxScore;
        }
    }
    
  • + 0 comments
    def twoStacks(maxSum, a, b):
        # Write your code here
        i = j = s = 0
        
        while i < len(a) and s + a[i] <= maxSum: 
            s += a[i]
            i += 1
            
        n = maxn = i
        i -= 1
        
        while j < len(b):
            if s + b[j] <= maxSum:
                s += b[j]
                j += 1
                n += 1
                maxn = max(maxn, n)
            elif i >= 0:
                   s -= a[i]
                   i -= 1
                   n -= 1
            else:
                break
        
        return maxn
    
  • + 1 comment

    I found the solution on programmingoneonone

  • + 0 comments
    def twoStacks(maxSum, a, b):
        Totalsum = 0
        countS1 = 0 
        countS2 = 0
        result = 0
        for ele in a:
            if Totalsum + ele > maxSum:
                break
            Totalsum = Totalsum + ele
            countS1 = countS1 + 1
        for ele in b:
            Totalsum = Totalsum + ele
            countS2 = countS2 + 1
            while Totalsum > maxSum and countS1 > 0:
                Totalsum = Totalsum - a[countS1-1]
                countS1 = countS1 -1
            if Totalsum <= maxSum:
                result = max((countS1 + countS2), result)
        return result
    
  • + 0 comments

    Question discription is missleading and thus the solutions I see in python, though passing, seems not completly correct. In the description:

    In each move, Nick can remove one integer from the top of either stack or stack .

    To my understanding, this means Nick can pick for a, then b and then a again. However from the examples and solutions in python, it seems that everyone are ignoring are ignoring it such possibility, and only including going first over 'a' and then 'b', but never going again back to 'a'