Equal Stacks

Sort by

recency

|

90 Discussions

|

  • + 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

  • + 0 comments

    In JAVA8 :

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
            // Write your code here
            long sum1 = h1.stream().mapToInt(Integer::intValue).sum();
            long sum2 = h2.stream().mapToInt(Integer::intValue).sum();
            long sum3 = h3.stream().mapToInt(Integer::intValue).sum();
            
            Collections.reverse(h1);
            Collections.reverse(h2);
            Collections.reverse(h3);
            
            Stack <Integer> s1 = new Stack<>();
            s1.addAll(h1);
            Stack <Integer> s2 = new Stack<>();
            s2.addAll(h2);
            Stack <Integer> s3 = new Stack<>();
            s3.addAll(h3);
            
            long minHeight;
            while(true){
                if (s1.isEmpty()||s2.isEmpty()||s3.isEmpty()) {
                    return 0;
                }
                
                minHeight = Math.min(sum1, Math.min(sum2, sum3));
                
                while (sum1 > minHeight) {
                    sum1 -= s1.pop();
                }
                while (sum2 > minHeight) {
                    sum2 -= s2.pop();
                }
                while (sum3 > minHeight) {
                    sum3 -= s3.pop();
                }
                
                if (sum1 == sum2 && sum2 == sum3) {
                    return (int)sum1;
                }
            }
        }
    
  • + 0 comments

    Typescript solution with a map. Can work with any count of incoming arrays

     
    function equalStacks(h1: number[], h2: number[], h3: number[]): number {
        let maxHeight:number = 0;
        let mapSimilar: Record<number, number> = {}
        let initArrays = [h1, h2, h3];
        function sumArray(values: number[]) { 
          let prev = 0;
          let value= 0;
          for(let i= values.length -1; i>=0; i--) {
            value= prev + values[i];
            if(!mapSimilar[value]) {
              mapSimilar[value] = 0;
            }
            mapSimilar[value]++;
            prev = value;
          } 
        }
        initArrays.forEach((subArray) => sumArray(subArray));
        for(const key in mapSimilar) {
          if(mapSimilar[key] === initArrays.length) {
            maxHeight = Math.max(maxHeight,  parseInt(key))
          }
        }
        return maxHeight;
    }