1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-20 19:03:47 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/使循环数组余额非负的最少移动次数(English) [minimum-moves-to-balance-circular-array].html
2025-12-17 09:38:38 +08:00

74 lines
3.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<p>You are given a <strong>circular</strong> array <code>balance</code> of length <code>n</code>, where <code>balance[i]</code> is the net balance of person <code>i</code>.</p>
<p>In one move, a person can transfer <strong>exactly</strong> 1 unit of balance to either their left or right neighbor.</p>
<p>Return the <strong>minimum</strong> number of moves required so that every person has a <strong>non-negative</strong> balance. If it is impossible, return <code>-1</code>.</p>
<p><strong>Note</strong>: You are guaranteed that <strong>at most</strong> 1 index has a <strong>negative</strong> balance initially.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">balance = [5,1,-4]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal sequence of moves is:</p>
<ul>
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [5, 0, -3]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [4, 0, -2]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [3, 0, -1]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [2, 0, 0]</code></li>
</ul>
<p>Thus, the minimum number of moves required is 4.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">balance = [1,2,-5,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal sequence of moves is:</p>
<ul>
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [1, 1, -4, 2]</code></li>
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -3, 2]</code></li>
<li>Move 1 unit from <code>i = 3</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -2, 1]</code></li>
<li>Move 1 unit from <code>i = 3</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, -1, 0]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 1</code>, resulting in <code>balance = [0, 1, -1, 0]</code></li>
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [0, 0, 0, 0]</code></li>
</ul>
<p>Thus, the minimum number of moves required is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">balance = [-3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p><strong></strong>It is impossible to make all balances non-negative for <code>balance = [-3, 2]</code>, so the answer is -1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == balance.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= balance[i] &lt;= 10<sup>9</sup></code></li>
<li>There is at most one negative value in <code>balance</code> initially.</li>
</ul>