Sort by

recency

|

1127 Discussions

|

  • + 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;
    

    }

  • + 0 comments
    from collections import deque
    
      h1, h2, h3 = deque(h1), deque(h2), deque(h3)
        sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3)
        while not (sum1 == sum2 == sum3):
            m = min(sum1, sum2, sum3)
            if sum1 > m:
                sum1 -= h1.popleft()
            elif sum2 > m:
                sum2 -= h2.popleft()
            elif sum3 > m:
                sum3 -= h3.popleft()
        return sum1