Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e89ed1a

Browse files
Merge pull request SharingSource#183 from SharingSource/ac_oier
✨feat: Add 453
2 parents eb079d5 + c18bee8 commit e89ed1a

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-four/solution/gong-shui-san-xie-zhuan-hua-wei-2-de-mi-y21lq/) | 简单 | 🤩🤩🤩 |
2121
| [441. 排列硬币](https://leetcode-cn.com/problems/arranging-coins/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arranging-coins/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-x-sv9o/) | 简单 | 🤩🤩🤩 |
2222
| [446. 等差数列划分 II - 子序列](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-ykvk/) | 困难 | 🤩🤩🤩🤩🤩 |
23+
| [453. 最小操作次数使数组元素相等](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-tt3zu/) | 中等 | 🤩🤩🤩 |
2324
| [470. 用 Rand7() 实现 Rand10()](https://leetcode-cn.com/problems/implement-rand10-using-rand7/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/gong-shui-san-xie-k-jin-zhi-zhu-wei-shen-zmd4/) | 中等 | 🤩🤩🤩🤩 |
2425
| [477. 汉明距离总和](https://leetcode-cn.com/problems/total-hamming-distance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/total-hamming-distance/solution/gong-shui-san-xie-ying-yong-cheng-fa-yua-g21t/) | 简单 | 🤩🤩🤩 |
2526
| [483. 最小好进制](https://leetcode-cn.com/problems/smallest-good-base/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/smallest-good-base/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-r94g/) | 困难 | 🤩🤩🤩🤩 |
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[453. 最小操作次数使数组元素相等](https://leetcode-cn.com/problems/minimum-moves-to-equal-array-elements/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-tt3zu/)** ,难度为 **简单**
4+
5+
Tag : 「数学」
6+
7+
8+
9+
给你一个长度为 `n` 的整数数组,每次操作将会使 `n - 1` 个元素增加 `1`
10+
11+
返回让数组所有元素相等的最小操作次数。
12+
13+
示例 1:
14+
```
15+
输入:nums = [1,2,3]
16+
17+
输出:3
18+
19+
解释:
20+
只需要3次操作(注意每次操作会增加两个元素的值):
21+
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
22+
```
23+
示例 2:
24+
```
25+
输入:nums = [1,1,1]
26+
27+
输出:0
28+
```
29+
30+
31+
提示:
32+
* n == nums.length
33+
* 1ドル <= nums.length <= 10^5$
34+
* $-10^9 <= nums[i] <= 10^9$
35+
* 答案保证符合 32-bit 整数
36+
37+
---
38+
39+
### 数学
40+
41+
为了方便,令原数组 $num$ 的总和为 $sum,ドル最小值为 $min,ドル最大值为 $max,ドル长度为 $n,ドル真实最小操作次数为 $ans$。
42+
43+
由于每次只能将 $n - 1$ 个元素整体加一,因此在最终的相等状态,整体元素的大小值 $t$ 满足关系 $t \geqslant max$。
44+
45+
我们考虑是否必然可以取到关系式中的等号?
46+
47+
答案是不一定,当且仅当 $num$ 本身有 $n - 1$ 个元素与 $max$ 差值相等,才能取得关系式中的等号。
48+
49+
同时我们知道,$ans$ 与 $t$ 存在一一对应关系:
50+
51+
$$
52+
ans = \frac{t * n - sum}{n - 1}
53+
$$
54+
55+
要取得最小的 $ans,ドル其实等价于取得最小的 $t,ドル但仅靠 $t \geqslant max$ 关系,我们无法直接求得 $ans$。
56+
57+
事实上,我们可以通过值变化来进行分析,凭直觉我们会觉得:**在配平整个数组的过程中,当前数组中的最小值会参与到自增过程中。**
58+
59+
我们通过「反证法」来证明该猜想的正确性。
60+
61+
假设在配平数组的过程,某次自增操作中,「当前最小值」没有参与到自增当中,那么此时自增的对象是除「当前最小值」以外的其余元素,这时候「当前最小值」与其他元素的差值将会增加 1ドル,ドル此时如果将操作换成「包含当前最小值自增」的话,我们是可以将最终值 $t$ 减一的,如果最终值 $t$ 变小的话,那么 $ans$ 也会变小,结果会更好。
62+
63+
**因此,如果我们某次自增操作中没有包含「当前最小值」对应的元素的话,我们可以通过调整 $t$ 的大小(减一),来将操作调整为「包含当前最小值进行自增」,同时结果会变好。**
64+
65+
到这里就结束了吗?
66+
67+
还没有,因为到这里我们还不能直接与原始最小值 $min$ 结合起来。
68+
69+
我们还需要证明 **原始的相对最小值 $min$ 在匹配过程中,可以一直保持自身是当前数组中的「相对最小值」**
70+
71+
这可以通过「归纳法」来证明:
72+
73+
**如果在每次自增操作中,都包含「当前最小值」,那么意味着原始最小值与其他元素的「相对大小」关系不会发生改变(因为原始最小值会一直作为「相对最小值」参与到每一次自增当中)得证成立。**
74+
75+
至此,我们可以得到 $t$ 和 $min$ 的关系式:
76+
77+
$$
78+
t = min + ans
79+
$$
80+
81+
代入之前我们得到的关系式可得:
82+
83+
$$
84+
ans = \frac{(min + ans) * n - sum}{n - 1}
85+
$$
86+
87+
变形整理后可得:
88+
89+
$$
90+
ans = sum - min * n
91+
$$
92+
93+
代码:
94+
```Java
95+
class Solution {
96+
public int minMoves(int[] nums) {
97+
int n = nums.length;
98+
int min = nums[0], sum = 0;
99+
for (int i : nums) {
100+
min = Math.min(min, i);
101+
sum += i;
102+
}
103+
return sum - min * n;
104+
}
105+
}
106+
```
107+
* 时间复杂度:$O(n)$
108+
* 空间复杂度:$O(1)$
109+
110+
---
111+
112+
### 最后
113+
114+
这是我们「刷穿 LeetCode」系列文章的第 `No.453` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /