Equal Stacks

Sort by

recency

|

97 Discussions

|

  • + 0 comments

    Imagine you have a pointer which always point to the smallest one. and try to remove the next one until it becomes the smallest. then repeat.

    function equalStacks(h1: number[], h2: number[], h3: number[]): number {
        let a = [h1, h2, h3]
        let s = a.map(x => x.reduce((a, b) => a + b, 0))
        let smallest = s[0];
        let smallestIndex = 0;
        for (let i = 0; i < a.length; i++) {
            if (s[i] < smallest) {
                smallest = s[i]
                smallestIndex = i
            }
        }
        
        while(!s.every(x => x == s[0])) {
            let i = (smallestIndex + 1) % a.length;
            while (s[i] > smallest) {
                s[i] -= a[i].shift()
                if (a[i].length == 0) {
                    return 0;
                }
            }
            if (s[i] <= smallest) {
                smallest = s[i];
                smallestIndex = i
            }
        }
        
        return smallest;
    }
    
  • + 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