Sort by

recency

|

1128 Discussions

|

  • + 0 comments
    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        Stack <Integer> stack1 = getStackFromList(h1);
        Stack <Integer> stack2 = getStackFromList(h2);
        Stack <Integer> stack3 = getStackFromList(h3);
        int s1 = getStackSum(stack1);
        int s2 = getStackSum(stack2);
        int s3 = getStackSum(stack3);
        int min = Math.min(s1, Math.min(s2,s3));
        while ((!(s1 == s2 && s1 == s3)) && min>0 ) {
            if (s1 > min) {
                stack1.pop();
                s1 = getStackSum(stack1);
            }
            if (s2 > min) {
                stack2.pop();
                s2 = getStackSum(stack2);
            }
            if (s3 > min) {
                stack3.pop();
                s3 = getStackSum(stack3);
            }
            min = Math.min(s1, Math.min(s2,s3));
            
        }
        return min;
    
        }
        
        static int getStackSum(Stack<Integer> input) {
           return input.stream()
             .mapToInt(Integer::intValue)
             .sum();
        }
        
        static Stack<Integer> getStackFromList(List<Integer> input) {
            Stack <Integer> stack = new Stack<>();
           for (int i = input.size() - 1; i >=0; i--) {
               stack.push(input.get(i));
           }
           return stack;
        }
    
  • + 1 comment

    EASY SOLUTION IN PYTHON3 WITH TWO APPROACHES

    def equalStacks(h1, h2, h3):
        # Write your code here
        
        # FIRST APPROACH
        s1, s2, s3 = sum(h1), sum(h2), sum(h3)
        ptr_1, ptr_2, ptr_3 = 0, 0, 0
        
        while not s1 == s2 == s3:
            if s1>=s2 and s1>=s3:
                s1 -= h1[ptr_1]
                ptr_1 += 1
            elif s2>=s1 and s2>=s3:
                s2 -= h2[ptr_2]
                ptr_2 += 1
            elif s3>=s1 and s3>=s2:
                s3 -= h3[ptr_3]
                ptr_3 += 1
    
        return s1
        
        # SECOND APPROACH
        d1, d2, d3 = deque(h1), deque(h2), deque(h3)
        s1, s2, s3 = sum(h1), sum(h2), sum(h3)
        
        while not s1 == s2 == s3:
            if s1>=s2 and s1>=s3:
                s1 -= d1.popleft()
            elif s2>=s1 and s2>=s3:
                s2 -= d2.popleft()
            elif s3>=s1 and s3>=s2:
                s3 -= d3.popleft()
                
        return s1
    
  • + 0 comments
    def equalStacks(h1, h2, h3):
        x1,x2,x3=sum(h1),sum(h2),sum(h3)
        while True:
            if x1==x2 and x2==x3:
                break
            elif x1>=x2 and x1>=x3:
                val=h1[0]
                x1-=val
                del h1[0]
            elif x3>=x2 and x3>=x1:
                val=h3[0]
                x3-=val
                del h3[0]
            elif x2>=x1 and x2>=x3:
                val=h2[0]
                x2-=val
                del h2[0]
        return x1
    
  • + 0 comments

    JavaScript (Node.js) Solution

    function equalStacks(h1, h2, h3) {
        let sum1 = h1.reduce((accumulator, currentValue) => accumulator + currentValue);
        let sum2 = h2.reduce((accumulator, currentValue) => accumulator + currentValue);
        let sum3 = h3.reduce((accumulator, currentValue) => accumulator + currentValue);
        let m;
        while (sum1 !== sum2 || sum2 !== sum3) {
            m =  Math.max(sum1, sum2 ,sum3);
            if (m === sum1) {
                sum1 -= (h1.shift());
            } else if (m === sum2) {
                sum2 -= (h2.shift());
            } else if (m === sum3) {
                sum3 -= (h3.shift());
            }
        }
        return sum1;
    }
    
  • + 0 comments

    I am not getting why it is showing wrong answer for some test cases

    class stck{ public: int sum; vector* ptr; };

    int equalStacks(vector h1, vector h2, vector h3) { stck* s1,*s2,*s3; s1=new stck; s2=new stck; s3=new stck; long long sm=0; for(int i=0;isum=sm; s1->ptr=&h1; sm=0; for(int i=0;isum=sm; s2->ptr=&h2; sm=0; for(int i=0;isum=sm; s3->ptr=&h3; int i,j,k; i=0,j=0,k=0;

    while(1){
    
        if(s1->sum>=s2->sum and s1->sum>s3->sum){
             s1->sum=s1->sum-(*s1->ptr)[i++];
         }
        else if(s2->sum>=s1->sum and s2->sum>s3->sum){
             s2->sum=s2->sum-(*s2->ptr)[j++];
            }
        else if(s3->sum>=s1->sum and s3->sum>s2->sum){
             s3->sum=s3->sum-(*s3->ptr)[k++];
    
            }
        else{break;}
        }
    return s1->sum;
    

    }