mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-12-20 19:03:47 +08:00
74 lines
3.2 KiB
HTML
74 lines
3.2 KiB
HTML
<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> </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> </p>
|
||
<p><strong>Constraints:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= n == balance.length <= 10<sup>5</sup></code></li>
|
||
<li><code>-10<sup>9</sup> <= balance[i] <= 10<sup>9</sup></code></li>
|
||
<li>There is at most one negative value in <code>balance</code> initially.</li>
|
||
</ul>
|