3776. Minimum Moves to Balance Circular Array
Asked by 1 company
Topics
You are given a circular array balance of length n, where balance[i] is the net balance of person i.
In one move, a person can transfer exactly 1 unit of balance to either their left or right neighbor.
Return the minimum number of moves required so that every person has a non-negative balance. If it is impossible, return -1.
Note: You are guaranteed that at most 1 index has a negative balance initially.
Example 1:
Input: balance = [5,1,-4]
Output: 4
Explanation:
One optimal sequence of moves is:
- Move 1 unit from
i = 1toi = 2, resulting inbalance = [5, 0, -3] - Move 1 unit from
i = 0toi = 2, resulting inbalance = [4, 0, -2] - Move 1 unit from
i = 0toi = 2, resulting inbalance = [3, 0, -1] - Move 1 unit from
i = 0toi = 2, resulting inbalance = [2, 0, 0]
Thus, the minimum number of moves required is 4.
Example 2:
Input: balance = [1,2,-5,2]
Output: 6
Explanation:
One optimal sequence of moves is:
- Move 1 unit from
i = 1toi = 2, resulting inbalance = [1, 1, -4, 2] - Move 1 unit from
i = 1toi = 2, resulting inbalance = [1, 0, -3, 2] - Move 1 unit from
i = 3toi = 2, resulting inbalance = [1, 0, -2, 1] - Move 1 unit from
i = 3toi = 2, resulting inbalance = [1, 0, -1, 0] - Move 1 unit from
i = 0toi = 1, resulting inbalance = [0, 1, -1, 0] - Move 1 unit from
i = 1toi = 2, resulting inbalance = [0, 0, 0, 0]
Thus, the minimum number of moves required is 6.
Example 3:
Input: balance = [-3,2]
Output: -1
Explanation:
It is impossible to make all balances non-negative for balance = [-3, 2], so the answer is -1.
Constraints:
1 <= n == balance.length <= 105-109 <= balance[i] <= 109- There is at most one negative value in
balanceinitially.
Hints
Hint 1
Hint 2
nums by their distance from the index with the negative value.Hint 3
nums to offset the current negative value.