You are viewing a single comment's thread. Return to all comments →
def equalStacks(h1, h2, h3): sum1, sum2, sum3 = sum(h1), sum(h2), sum(h3) i, j, k = 0, 0, 0 while not (sum1 == sum2 == sum3): if sum1 > sum2 or sum1 > sum3: sum1 -= h1[i] i += 1 elif sum2 > sum1 or sum2 > sum3: sum2 -= h2[j] j += 1 elif sum3 > sum1 or sum3 > sum2: sum3 -= h3[k] k += 1 if i == len(h1) or j == len(h2) or k == len(h3): return 0 return sum1
or
def equalStacks(h1, h2, h3): sum_h1, sum_h2, sum_h3 = sum(h1), sum(h2), sum(h3) while sum_h1 != sum_h2 or sum_h2 != sum_h3: if sum_h1 > sum_h2 or sum_h1 > sum_h3: sum_h1 -= h1.pop(0) elif sum_h2 > sum_h3: sum_h2 -= h2.pop(0) else: sum_h3 -= h3.pop(0) return sum_h1
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
or