Equal Stacks

Sort by

recency

|

92 Discussions

|

  • + 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})
    
  • + 0 comments

    Java 8 - Based on all way subtracting a cylinder from the highest stack. This naturally protects from indexing exceptions and negative values since 0,0,0 is always true.

        public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
            int s1h = h1.stream().collect(Collectors.summingInt(Integer::intValue));
            int s2h = h2.stream().collect(Collectors.summingInt(Integer::intValue));
            int s3h = h3.stream().collect(Collectors.summingInt(Integer::intValue));
            
            int s1i=0,s2i=0,s3i=0;
            
            while(s1h != s2h || s1h != s3h) {
                if (s1h>=s2h && s1h>=s3h )
                    s1h -= h1.get(s1i++);
                else if (s2h>=s1h && s2h>=s3h )
                    s2h -= h2.get(s2i++);
                else if (s3h>=s1h && s3h>=s2h )
                    s3h -= h3.get(s3i++);
            }
            return s1h;
        }
    
  • + 0 comments

    I use a map for elements index and sums and increment index on the list with the lowest sum each loop

    time O(n) space O(1)

    def equalStacks(h1, h2, h3):
        # Write your code here
        max_val = 0
        index = {1: -1, 2: -1, 3: -1}
        sums = {1: 0, 2: 0, 3: 0}
        h1.reverse()
        h2.reverse()
        h3.reverse()
        list_map = {1: h1, 2: h2, 3: h3}
        while True:
            if not index:
                break
            next_index_sum = min([v for k, v in sums.items() if k in index])
            list_key = {v: k for k, v in sums.items() if k in index}[next_index_sum]
            next_block_index = index[list_key] +1
            if next_block_index <= list_map[list_key].__len__() -1:
                sums[list_key] += list_map[list_key][next_block_index]
                index[list_key] +=1
            else:
                del index[list_key]
            if len(set(sums.values())) == 1:
                max_val = max(max_val, sums[1])
        return max_val
                
    
  • + 0 comments

    Here is the approch for Time complexity O(n) and space complexity O(1).

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        // Write your code here
            long sum1 = 0;
            for(Integer h: h1)
                sum1 += h;
                
            long sum2 = 0;
            for(Integer h: h2)
                sum2 += h;
                
            long sum3 = 0;
            for(Integer h: h3)
                sum3 += h;
                
            //   H1    H2   H3
            int i = 0, j=0, k=0;
             
            // System.out.println("i "+ i + " j " +j +" k "+ k); 
            // System.out.println("h1 "+ sum1 + " h2 " +sum2 +" h3 "+ sum3); 
            while((i < h1.size() && j < h2.size() && k < h3.size()) 
                && (sum1 != sum2 || sum2 != sum3 || sum1 != sum3)) {
                
                if(sum1 > sum2) { // sum1
                    if(sum1 > sum3) { // sum1
                        sum1 -= h1.get(i);
                        i++;
                    } else if(sum3 > sum1) { //sum3
                        sum3 -= h3.get(k);
                        k++;
                    }else { //sum3 and sum1
                        sum1 -= h1.get(i);
                        i++;
                        sum3 -= h3.get(k);
                        k++;
                    }
                } else if(sum2 > sum1) { // sum2
                    if(sum2 > sum3) { // sum2
                        sum2 -= h2.get(j);
                        j++;
                    } else if(sum3 > sum2) { //sum3
                        sum3 -= h3.get(k);
                        k++;
                    }else { //sum2 and sum1
                        sum2 -= h2.get(j);
                        j++;
                        sum3 -= h3.get(k);
                        k++;
                    }
                }  
                else if( sum2 > sum3 ) { //sum2
                    if(sum2 > sum1) { // sum2
                        sum2 -= h2.get(j);
                        j++;
                    } else if(sum1 > sum2) { //sum1
                        sum1 -= h1.get(i);
                        i++;
                    }else { //sum2 and sum1
                        sum2 -= h2.get(j);
                        j++;
                        sum1 -= h1.get(i);
                        i++;
                    }
                } else if( sum3 > sum2) { // sum3
                     if(sum3 > sum1) { // sum3
                        sum3 -= h3.get(k);
                        k++;
                    } else if(sum1 > sum3) { //sum1
                        sum1 -= h1.get(i);
                        i++;
                    }else { //sum2 and sum1
                        sum3 -= h3.get(k);
                        k++;
                        sum1 -= h1.get(i);
                        i++;
                    }
                } else if(sum1 > sum3) { // sum1
                    if(sum1 > sum2) { // sum1
                        sum1 -= h1.get(i);
                        i++;
                    } else if(sum2 > sum1) { //sum3
                        sum2 -= h2.get(j);
                        j++;
                    }else { //sum3 and sum1
                        sum1 -= h1.get(i);
                        i++;
                        sum2 -= h2.get(j);
                        j++;
                    }
                } else if(sum3 > sum1) { // sum3
                    if(sum3 > sum2) { // sum3
                        sum3 -= h3.get(k);
                        k++;
                    } else if(sum2 > sum3) { //sum2
                        sum2 -= h2.get(j);
                        j++;
                    }else { //sum2 and sum3
                        sum3 -= h3.get(k);
                        k++;
                        sum2 -= h2.get(j);
                        j++;
                    }
                } 
                // System.out.println("i "+ i + " j " +j +" k "+ k); 
                // System.out.println("h1 "+ sum1 + " h2 " +sum2 +" h3 "+ sum3); 
            }
            if(sum1 == sum2 && sum2 == sum3)
                return (int)sum1;
            return 0;
        }
    
  • + 0 comments

    Here is - HackerRank Equal Stacks problem solution in Python, Java, C++, C and javascript