Equal Stacks

Sort by

recency

|

33 Discussions

|

  • + 0 comments

    Python3 set intersect:

    def equalStacks(h1, h2, h3):
        s1 = set(accumulate(reversed(h1), lambda s, n: s + n))
        s2 = set(accumulate(reversed(h2), lambda s, n: s + n))
        s3 = set(accumulate(reversed(h3), lambda s, n: s + n))
        
        return max(s1 & s2 & s3, default=0)
    
  • + 0 comments

    Java O(n)

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
        int l1=0,l2=0,l3=0;
    
        Stack<Integer> h11= new Stack<Integer>();
        Stack<Integer> h12= new Stack<Integer>();
        Stack<Integer> h13= new Stack<Integer>();
    
        for(int i=h1.size()-1;i>=0;i--){
            l1+=h1.get(i);
            h11.push(h1.get(i));
        }
        for(int i=h2.size()-1;i>=0;i--){
            l2+=h2.get(i);
            h12.push(h2.get(i));
        }
        for(int i=h3.size()-1;i>=0;i--){
            l3+=h3.get(i);
            h13.push(h3.get(i));
        }
    
        while( l1!=l2 || l2!=l3){
    
            if(l1>l2 || l1>l3){
                l1-=h11.pop();
            }
            if(l2>l1 || l2>l3) {
                l2 -= h12.pop();
            }
            if(l3>l2 || l3>l1){
                l3-=h13.pop();
            }
        }
        return l1;
    }
    
  • + 0 comments

    Java O(n1 + n2 + n3)

    public static int equalStacks(List<Integer> h1, List<Integer> h2, List<Integer> h3) {
            Stack<Integer> stack1 = new Stack<>();
            Stack<Integer> stack2 = new Stack<>();
            Stack<Integer> stack3 = new Stack<>();
    
            int sum1 = 0, sum2 = 0, sum3 = 0;
    
            for (int i = h1.size() - 1; i >= 0; i--) {
                stack1.push(h1.get(i));
                sum1 += h1.get(i);
            }
    
            for (int i = h2.size() - 1; i >= 0; i--) {
                stack2.push(h2.get(i));
                sum2 += h2.get(i);
            }
    
            for (int i = h3.size() - 1; i >= 0; i--) {
                stack3.push(h3.get(i));
                sum3 += h3.get(i);
            }
    
            while (!stack1.isEmpty() && !stack2.isEmpty() && !stack3.isEmpty()) {
                int minSum = Math.min(sum1, Math.min(sum2, sum3));
    
                while (sum1 > minSum) sum1 -= stack1.pop();
                while (sum2 > minSum) sum2 -= stack2.pop();
                while (sum3 > minSum) sum3 -= stack3.pop();
    
                if (sum1 == sum2 && sum2 == sum3) return sum1;
            }
    
            return 0; 
        }
    
  • + 0 comments
    def equalStacks(h1, h2, h3):
        s1, s2, s3 = sum(h1), sum(h2), sum(h3)
        h1.reverse()
        h2.reverse()
        h3.reverse()
        while True:
            minimum = min(s1, s2, s3)
            if not minimum:
                return minimum
            s1 -= h1.pop() if s1 > minimum else 0
            s2 -= h2.pop() if s2 > minimum else 0
            s3 -= h3.pop() if s3 > minimum else 0
            if s1 == s2 == s3:
                return s1
    
  • + 0 comments

    C#

        public static int equalStacks(List<int> h1, List<int> h2, List<int> h3)
        {
            int sum1 = h1.Sum();
            int h1Index = 0;
            int sum2 = h2.Sum();
            int h2Index = 0;
            int sum3 = h3.Sum();
            int h3Index = 0;
            
            if (sum1 == 0 || sum2 == 0 || sum3 == 0) return 0;
            while (sum1 != sum2 || sum2 != sum3 || sum1 != sum3) {
                if (h1Index >= h1.Count() || h2Index >= h2.Count() 
                    || h3Index >= h3.Count()) return 0;
                    
                int max = (int)Math.Max(Math.Max(sum1, sum2), sum3);
                if (sum1 == max)
                    sum1 -= h1[h1Index++];
                else if (sum2 == max)
                    sum2 -= h2[h2Index++];
                else
                    sum3 -= h3[h3Index++];
            }
            return sum1;
        }