Sort by

recency

|

1140 Discussions

|

  • + 0 comments

    Python 3:

    def equalStacks(h1, h2, h3): # Write your code here

    h1 = h1[::-1]
    h2 = h2[::-1]
    h3 = h3[::-1]
    
    h1hite = sum(h1)
    h2hite = sum(h2)
    h3hite = sum(h3)
    
    while True:
    
        if h1hite == h2hite and h1hite == h3hite:
            break
        else:
            if h1hite >= h2hite and h1hite >= h3hite:
                x = h1.pop()
                h1hite -= x
            elif h2hite >= h1hite and h2hite >= h3hite:
                x = h2.pop()
                h2hite -= x
            elif h3hite >= h1hite and h3hite >= h2hite:
                x = h3.pop()
                h3hite -= x
    
    return h1hite
    
  • + 0 comments

    Beat Google AI for this one :)

    from itertools import accumulate

    def equalStacks(h1, h2, h3): # Write your code here

    h1a = set(accumulate(h1[::-1]))
    h2a = set(accumulate(h2[::-1]))
    h3a = set(accumulate(h3[::-1]))
    
    return max(h1a.intersection(h2a).intersection(h3a).union({0}))
    
  • + 0 comments

    Java8 with steams

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        // Write your code here
            // there are 3 stacks involved
            List<Integer> cumulHeights = new ArrayList<>();
            Integer cumul = 0;
            for (int i = h1.size() - 1; i>=0; i--) {
                cumul += h1.get(i);
                cumulHeights.add(cumul);
            }
            cumul = 0;
            for (int i = h2.size() - 1; i>=0; i--) {
                cumul += h2.get(i);
                cumulHeights.add(cumul);
            }
            cumul = 0;
            for (int i = h3.size() - 1; i>=0; i--) {
                cumul += h3.get(i);
                cumulHeights.add(cumul);
            }
            Optional<Map.Entry<Integer, Long>> mm = cumulHeights.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting())).entrySet()
                    .stream().filter((s) -> s.getValue() == 3).max(Comparator.comparingInt(s -> s.getKey()));
            if (mm.isPresent()) {
                return mm.get().getKey();
            }
            return 0;
        }
    
  • + 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); 
    }