Equal Stacks

Sort by

recency

|

96 Discussions

|

  • + 0 comments

    I do not understand the following explanation for: h1 = [1,2,1,1] h2 = [1,1,2] h3 = [1,1] There are 4, 3, and 2 cylinders in the three stacks, with their heights in the three arrays. Remove the top 2 cylinders from h1 (heights = [1, 2]) and from h2 (heights = [1, 1]) so that the three stacks all are 2 units tall. Return 2 as the answer.

    h1 (heights = [1, 2]) has height 3, while h2 (heights = [1, 1]) has height 2, so the answer should be 1

  • + 0 comments

    JavaScript Soution:

    function equalStacks(h1, h2, h3) {
      let h1Total = h1.reduce((a, c) => a + c);
      let h2Total = h2.reduce((a, c) => a + c);
      let h3Total = h3.reduce((a, c) => a + c);
    
      function checkEquality(one, two, three) {
        if (one === two && two === three) {
          return true;
        }
        return false;
      }
    
      while (!checkEquality(h1Total, h2Total, h3Total)) {
        let numberToSubtract = 0;
        if (h1Total >= h2Total && h1Total >= h3Total) {
          numberToSubtract = h1.shift();
          h1Total -= numberToSubtract;
        } else if (h2Total >= h1Total && h2Total >= h3Total) {
          numberToSubtract = h2.shift();
          h2Total -= numberToSubtract;
        } else if (h3Total >= h1Total && h3Total >= h1Total) {
          numberToSubtract = h3.shift();
          h3Total -= numberToSubtract;
        }
      }
    
      return h1Total;
    }
    
  • + 0 comments

    It's readable

    def equalStacks(h1, h2, h3): h1.reverse() h2.reverse() h3.reverse()

    stacks = [h1, h2, h3]
    
    heights = [sum(stack) for stack in stacks]
    while True:
        if len(set(heights)) == 1:
            return heights[0]
        else:
            idx = heights.index(max(heights))
            heights[idx] -= stacks[idx].pop()   
    
  • + 0 comments

    def equalStacks(h1, h2, h3):

    sums = [sum(h1), sum(h2), sum(h3)]
    stacks = [h1, h2, h3]
    
    t = min(sums)
    
    while sums[0] != sums[1] or sums[1] != sums[2] or sums[0] != sums[2]: 
    
        for i in range(len(sums)):
            while sums[i] > t:
                sums[i] -= stacks[i].pop(0)   
    
        t = min(sums)
    
    return t
    
  • + 0 comments
    from itertools import accumulate as a
    
    def equalStacks(h1, h2, h3):
        return max(set(a(h1[::-1])) & set(a(h2[::-1])) & set(a(h3[::-1])) | {0})