Sort by

recency

|

1132 Discussions

|

  • + 0 comments

    using javascript

    function equalStacks(h1, h2, h3) {
      let size1 = 0,
        size2 = 0,
        size3 = 0,
        maxSize = 0;
      while (true) {
        if (size1 === size2 && size2 === size3) maxSize = size1;
        if (!h1.length && !h2.length && !h3.length) {
          break;
        }
        
        if (size1 <= size2 && size1 <= size3 && h1.length) {
          size1 += h1.pop();
        } else if (size2 <= size1 && size2 <= size3 && h2.length) {
          size2 += h2.pop();
        } else if (size3 <= size1 && size3 <= size2 && h3.length) {
          size3 += h3.pop();
        }else break
      }
    
      return maxSize;
    }
    
  • + 0 comments

    Here is my C++ solution:

    int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3) {
    
        auto ref = [&](){
            std::vector<vector<int>*> v_;
            v_.push_back(&h1);
            v_.push_back(&h2);
            v_.push_back(&h3);
            return v_;
        }();
    
        int hight[3]{};
        for (auto it : h1) hight[0] += it;
        for (auto it : h2) hight[1] += it;
        for (auto it : h3) hight[2] += it;
    
        while (true) {
            if (hight[0] == hight[1] && hight[0] == hight[2]) return hight[0];
    
            int indx = 0;
            if (hight[1] > hight[indx]) indx = 1;
            else if (hight[2] > hight[indx]) indx = 2;
            
            auto el = ref[indx]->front();
            ref[indx]->erase(ref[indx]->begin());
            hight[indx] -= el;
        }
    
        return hight[0];
    }
    
  • + 0 comments

    public static int highest(int s1, int s2, int s3){ return Math.max(s1,Math.max(s2,s3)); }

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
    // Write your code here
    int n1=h1.size();
    int n2=h2.size();
    int n3=h3.size();
    int s1=0;
    int s2=0;
    int s3=0;
    int high;
    int p1=0; 
    int p2=0;
    int p3=0;
    
    for(int i=0;i<n1;i++){
        s1+=h1.get(i);
    }
    for(int i=0;i<n2;i++){
        s2+=h2.get(i);
    }
    for(int i=0;i<n3;i++){
        s3+=h3.get(i);
    }
    
    while(p1<h1.size() && p2<h2.size() && p3<h3.size()){
    
        if(s1==s2 && s2==s3){
            return s1;
        }
    
        else{
            high=highest(s1,s2,s3);
    
            if(high==s1){
                s1-=h1.get(p1);
                p1++;
            }
            if(high==s2){
                s2-=h2.get(p2);
                p2++;
            }
            if(high==s3){
                s3-=h3.get(p3);
                p3++;
            }
        }
    }
    
    return 0;
    

    }

    }

  • + 0 comments

    int equalStacks(vector h1, vector h2, vector h3) { int n1= accumulate (h1.begin(),h1.end(),0); int n2= accumulate (h2.begin(),h2.end(),0); int n3= accumulate (h3.begin(),h3.end(),0);

    while(true)
    {
        if (n1==n2 && n2==n3)
        {
            break;
        }
    
    
        if (n1>=n2 && n1>=n3)
        {
            n1=n1-h1.front();
            h1.erase(h1.begin());
        }
    
        else if (n2>=n1 && n2>=n3)
        {
            n2=n2-h2.front();
            h2.erase(h2.begin());
        }
    
        else if (n3>=n1 && n3>=n1)
        {
            n3=n3-h3.front();
            h3.erase(h3.begin());
        }
    
    }
    
    return n1;
    

    }

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