본문 바로가기

hacking sorcerer

Problem Solving: Balance the Boxes

728x90
반응형

                         
def min_moves_to_balance(boxes):
    n = len(boxes)
    t = sum(boxes)
    if t % n != 0:
        return -1
    u = t // n

    moves = 0
    d = 0
    for i in range(n - 1):
        d += boxes[i] - u      # running imbalance
        moves += abs(d)        # balls crossing boundary i->i+1
    return moves

boxes = list(map(int, input().split()))
print(min_moves_to_balance(boxes))

728x90
반응형

'hacking sorcerer' 카테고리의 다른 글

tryhackme bankgpt  (0) 2025.12.17
not thy beautiful  (0) 2025.12.17
Problem: Balance the Boxes  (0) 2025.12.16
really beautiful  (0) 2025.12.10
Problem: Valley Count in a Number Path  (0) 2025.12.02