Sort by

recency

|

1137 Discussions

|

  • + 0 comments

    C++ Solution with unlimited input stacks

    int equalStacks(std::vector<int> h1, std::vector<int> h2, std::vector<int> h3) {
        std::multimap<int, std::vector<int>> stacks {  };
        stacks.insert({ std::accumulate(h1.begin(), h1.end(), 0), h1 });
        stacks.insert({ std::accumulate(h2.begin(), h2.end(), 0), h2 });
        stacks.insert({ std::accumulate(h3.begin(), h3.end(), 0), h3 });
        
        while (stacks.count(stacks.begin()->first) != stacks.size()) {
            auto max_sum { stacks.rbegin()->first };
            auto max_stacks { stacks.equal_range(max_sum) };
            auto max_stacks_copy { std::vector<decltype(stacks)::iterator>() };
            
            for (auto it = max_stacks.first; it != max_stacks.second; ++it) {
                max_stacks_copy.push_back(it);
            }
            
            for (auto it : max_stacks_copy) {    
                auto node { stacks.extract(it->first) };            
                node.key() -= *node.mapped().begin();
                node.mapped().erase(node.mapped().begin());
                
                stacks.insert(std::move(node));
            }
        }
        
        return stacks.begin()->first;
    }
    
  • + 0 comments

    C++ solution:

    int getSum(vector<int> h);
    
    int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3) {      
        int h1sum = getSum(h1);
        int h2sum = getSum(h2);
        int h3sum = getSum(h3);
        
        while (h1.size() && h2.size() && h3.size() && !(h1sum == h2sum && h1sum == h3sum)) {
            auto maxVal = max({ h1sum, h2sum, h3sum });
            if (maxVal == h1sum) {
                h1sum -= h1.at(0);
                h1.erase(h1.begin());
            }
            else if (maxVal == h2sum) {
                h2sum -= h2.at(0);
                h2.erase(h2.begin());
            }
            else {
                h3sum -= h3.at(0);
                h3.erase(h3.begin());
            }
        }
    
        if (h1.size() && h2.size() && h3.size()) {
            return h1sum;
        }
        else {
            return 0;
        }
    }
    
    int getSum(vector<int> h){
       return accumulate(h.begin(), h.end(), 0); 
    }
    
  • + 0 comments

    JAVA

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
            int s1 = h1.stream().mapToInt(x -> x.intValue()).sum();
            int s2 = h2.stream().mapToInt(x -> x.intValue()).sum();
            int s3 = h3.stream().mapToInt(x -> x.intValue()).sum();
            Collections.reverse(h1);
            Collections.reverse(h2);
            Collections.reverse(h3);
            while(s1 != s2 || s2 != s3){
                if(s1 > s2 || s1 > s3){
                    s1 -= h1.remove(h1.size() - 1);
                }
                
                if(s2 > s1 || s2 > s3){
                    s2 -= h2.remove(h2.size() - 1);
                }
                
                if(s3 > s2 || s3 > s1){
                    s3 -= h3.remove(h3.size() - 1);
                }
                
            }
            return s1;
        }
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'equalStacks' function below.

    #

    The function is expected to return an INTEGER.

    The function accepts following parameters:

    1. INTEGER_ARRAY h1

    2. INTEGER_ARRAY h2

    3. INTEGER_ARRAY h3

    #

    def equalStacks(h1, h2, h3): sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3) i, j, k = 0, 0, 0

    while not (sum1 == sum2 == sum3):  
        if sum1 > sum2 or sum1 > sum3:
            sum1 -= h1[i]
            i += 1
        elif sum2 > sum1 or sum2 > sum3:
            sum2 -= h2[j]
            j += 1
        elif sum3 > sum1 or sum3 > sum2:
            sum3 -= h3[k]
            k += 1
        if i == len(h1) or j == len(h2) or k == len(h3):
            return 0
    return sum1
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()
    
    n1 = int(first_multiple_input[0])
    
    n2 = int(first_multiple_input[1])
    
    n3 = int(first_multiple_input[2])
    
    h1 = list(map(int, input().rstrip().split()))
    
    h2 = list(map(int, input().rstrip().split()))
    
    h3 = list(map(int, input().rstrip().split()))
    
    result = equalStacks(h1, h2, h3)
    
    fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 1 comment
    def equalStacks(h1, h2, h3):
        sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3)
        i, j, k = 0, 0, 0
        
        while not (sum1 == sum2 == sum3):  
            if sum1 > sum2 or sum1 > sum3:
                sum1 -= h1[i]
                i += 1
            elif sum2 > sum1 or sum2 > sum3:
                sum2 -= h2[j]
                j += 1
            elif sum3 > sum1 or sum3 > sum2:
                sum3 -= h3[k]
                k += 1
            if i == len(h1) or j == len(h2) or k == len(h3):
                return 0
        return sum1
    

    or

    def equalStacks(h1, h2, h3):
        sum_h1, sum_h2, sum_h3 = sum(h1), sum(h2), sum(h3)
        
        while sum_h1 != sum_h2 or sum_h2 != sum_h3:
            if sum_h1 > sum_h2 or sum_h1 > sum_h3:
                sum_h1 -= h1.pop(0)
            elif sum_h2 > sum_h3:
                sum_h2 -= h2.pop(0)
            else:
                sum_h3 -= h3.pop(0)
        
        return sum_h1