Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4004.Minimum%20Moves%20to%20Balance%20Circular%20Array%20II/README.md
---

<!-- problem:start -->

# [4004. 使循环数组余额非负的最少移动次数 II 🔒](https://leetcode.cn/problems/minimum-moves-to-balance-circular-array-ii)

[English Version](/solution/4000-4099/4004.Minimum%20Moves%20to%20Balance%20Circular%20Array%20II/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给定一个长度为 <code>n</code> 的 <span data-keyword="circular-array">环形数组</span> <code>balance</code>,其中 <code>balance[i]</code>&nbsp;是第 <code>i</code> 个人的净余额。</p>

<p>在一次操作中,一个人可以向其左侧或右侧的相邻人员转移&nbsp;<strong>恰好</strong> 1 单位的余额。</p>

<p>返回使每个人的余额都变为&nbsp;<strong>非负&nbsp;</strong>所需的&nbsp;<strong>最少&nbsp;</strong>操作次数。如果无法做到,则返回 -1。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">balance = [-1,2,-1]</span></p>

<p><strong>输出:</strong><span class="example-io">2</span></p>

<p><strong>解释:</strong></p>

<p>一种最优的操作序列如下:</p>

<ul>
<li>从 <code>i = 1</code> 向 <code>i = 0</code> 转移 1 单位余额,得到 <code>balance = [0, 1, -1]</code></li>
<li>从 <code>i = 1</code> 向 <code>i = 2</code> 转移 1 单位余额,得到 <code>balance = [0, 0, 0]</code></li>
</ul>

<p>因此,所需的最少操作次数为 2。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">balance = [4,-1,-2]</span></p>

<p><strong>输出:</strong><span class="example-io">3</span></p>

<p><strong>解释:</strong></p>

<p>一种最优的操作序列如下:</p>

<ul>
<li>从 <code>i = 0</code> 向 <code>i = 1</code> 转移 1 单位余额,得到 <code>balance = [3, 0, -2]</code></li>
<li>从 <code>i = 0</code> 向 <code>i = 2</code> 转移 1 单位余额,得到 <code>balance = [2, 0, -1]</code></li>
<li>从 <code>i = 0</code> 向 <code>i = 2</code> 再转移 1 单位余额,得到 <code>balance = [1, 0, 0]</code></li>
</ul>

<p>因此,所需的最少操作次数为 3。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">balance = [-3,-3,5]</span></p>

<p><strong>输出:</strong><span class="example-io">-1</span></p>

<p><strong>解释:</strong></p>

<p>对于 <code>balance = [-3, -3, 5]</code>,无法使所有人的余额都变为非负,因此答案为 -1。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= n == balance.length &lt;= 1000</code></li>
<li><code>-10<sup>5</sup> &lt;= balance[i] &lt;= 10<sup>5</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4004.Minimum%20Moves%20to%20Balance%20Circular%20Array%20II/README_EN.md
---

<!-- problem:start -->

# [4004. Minimum Moves to Balance Circular Array II 🔒](https://leetcode.com/problems/minimum-moves-to-balance-circular-array-ii)

[中文文档](/solution/4000-4099/4004.Minimum%20Moves%20to%20Balance%20Circular%20Array%20II/README.md)

## Description

<!-- description:start -->

<p>You are given a <span data-keyword="circular-array">circular array</span> <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 -1.</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 = [-1,2,-1]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</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 = 0</code>, resulting in <code>balance = [0, 1, -1]</code></li>
<li>Move 1 unit from <code>i = 1</code> to <code>i = 2</code>, resulting in <code>balance = [0, 0, 0]</code></li>
</ul>

<p>Thus, the minimum number of moves required is 2.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">balance = [4,-1,-2]</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>One optimal sequence of moves is:</p>

<ul>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 1</code>, resulting in <code>balance = [3, 0, -2]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [2, 0, -1]</code></li>
<li>Move 1 unit from <code>i = 0</code> to <code>i = 2</code>, resulting in <code>balance = [1, 0, 0]</code></li>
</ul>

<p>Thus, the minimum number of moves required is 3.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">balance = [-3,-3,5]</span></p>

<p><strong>Output:</strong> <span class="example-io">-1</span></p>

<p><strong>Explanation:</strong></p>

<p>It is impossible to make all balances non-negative for <code>balance = [-3, -3, 5]</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;= 1000</code></li>
<li><code>-10<sup>5</sup> &lt;= balance[i] &lt;= 10<sup>5</sup></code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4005.Minimum%20Operations%20to%20Make%20Array%20Equal%20III/README.md
---

<!-- problem:start -->

# [4005. Minimum Operations to Make Array Equal III 🔒](https://leetcode.cn/problems/minimum-operations-to-make-array-equal-iii)

[English Version](/solution/4000-4099/4005.Minimum%20Operations%20to%20Make%20Array%20Equal%20III/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given an integer array <code>nums</code>.</p>

<p>In one operation, you may choose <strong>any</strong> element <code>nums[i]</code> and perform one of the following:</p>

<ul>
<li><strong>Multiply</strong> <code>nums[i]</code> by an integer <code>k</code>, where <code>k &gt;= 2</code>.</li>
<li><strong>Divide</strong> <code>nums[i]</code> by an integer <code>k</code>, where <code>2 &lt;= k &lt; nums[i]</code>, provided that <code>nums[i]</code> is divisible by <code>k</code>.</li>
</ul>

<p>Return the <strong>minimum</strong> number of operations required to make all elements of <code>nums</code> <strong>equal</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [6,12,8]</span></p>

<p><strong>Output:</strong> 3</p>

<p><strong>Explanation:</strong></p>

<p>We can perform following operates to make all numbers to 6:</p>

<ul>
<li>Divide <code>nums[1] = 12</code> by 2 to get 6.</li>
<li>Divide <code>nums[2] = 8</code> by 4 to get 2.</li>
<li>Multiply <code>nums[2] = 2</code> by 3 to get 6.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,15,20]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>We can perform following operates to make all numbers to 5:</p>

<ul>
<li>Divide <code>nums[1] = 15</code> by 3 to get 5.</li>
<li>Divide <code>nums[2] = 20</code> by 4 to get 5.</li>
</ul>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [7,7,7]</span></p>

<p><strong>Output:</strong> <span class="example-io">0</span></p>

<p><strong>Explanation:</strong></p>

<p>All elements are already equal, so no operations are needed.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>​​​​​​​9</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading