Sort by

recency

|

413 Discussions

|

  • + 0 comments
    {
        stack<int> a1;
        stack<int> b1;
        int val = 0;
        int tmp = 0;
        int t = 0;
        for (int i = a.size() - 1; i >= 0; i--)
        {
            a1.push(a[i]);
        }
        for (int i = b.size() - 1; i >= 0; i--)
        {
            b1.push(b[i]);
        }
    
        while (tmp <= maxSum && (!a1.empty() || !b1.empty()))
        {
            val = t;
    
            if (!a1.empty() && (b1.empty() || a1.top() <= b1.top()))
            {
                tmp += a1.top();
                a1.pop();
                t += 1;
            }
            else if (!b1.empty())
            {
                tmp += b1.top();
                b1.pop();
                t += 1;
            }
        }
        return val;
    }
    

    what is the problem

  • + 0 comments

    You just nailed it. I was looking it for my gaming project.

  • + 0 comments

    This question is very poorly described , and its misleading once you go from one stack to other you can't go back but its not mentioned properly

  • + 0 comments

    guys this is not a Greedy question where we can pick min of both the stacks. that doesn't work. We need to try all the possibilities. Recursion & Backtracking

  • + 1 comment

    Only 2 test cases (0,4)are passing all other are failing , can't think where is it going wrong public static int solve(Stack f,Stack s,int len , int cs,int max){ int n = f.peek()>=s.peek()?s.peek():f.peek(); // System.out.println(f.peek()); // System.out.println(s.peek()); // System.out.println("n:"+n); if(cs+n a, List b) { // Write your code here Stack first = new Stack<>(); for(int i =a.size()-1;i>=0;i--){ first.push(a.get(i)); } // System.out.println;

    Stack<Integer> Second = new Stack<>();
    for(int i =b.size()-1;i>=0;i--){
        Second.push(b.get(i));
    }
    int len = 0;
    int currsumm = 0;
    return solve(first,Second,len,currsumm,maxSum);
    
    }
    

    }