Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Third solution in Clear category for Can Balance by kazuki.h
def can_balance(weights):
left_weight_sums, right_weight_sums = [0], [0]
sum_L = SUM_L = sum_R = SUM_R = 0
for w in weights:
sum_L += w
SUM_L += sum_L
left_weight_sums.append(SUM_L)
for w in weights[::-1]:
sum_R += w
SUM_R += sum_R
right_weight_sums.append(SUM_R)
for i, (w_l, w_r) in enumerate(zip(left_weight_sums, right_weight_sums[-2::-1])):
if w_l == w_r: return i
return -1
Aug. 9, 2022
Comments: