Equal Stacks

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