Skip to content

Commit 86370c1

Browse files
committed
Add solution 0462
1 parent be276a5 commit 86370c1

26 files changed

+659
-438
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func minMoves2(nums []int) int {
9+
if len(nums) == 0 {
10+
return 0
11+
}
12+
moves, mid := 0, len(nums)/2
13+
sort.Ints(nums)
14+
for i := range nums {
15+
if i == mid {
16+
continue
17+
}
18+
moves += int(math.Abs(float64(nums[mid] - nums[i])))
19+
}
20+
return moves
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question462 struct {
9+
para462
10+
ans462
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para462 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans462 struct {
22+
one int
23+
}
24+
25+
func Test_Problem462(t *testing.T) {
26+
27+
qs := []question462{
28+
29+
{
30+
para462{[]int{}},
31+
ans462{0},
32+
},
33+
34+
{
35+
para462{[]int{1, 2, 3}},
36+
ans462{2},
37+
},
38+
39+
{
40+
para462{[]int{1, 10, 2, 9}},
41+
ans462{16},
42+
},
43+
44+
{
45+
para462{[]int{1, 0, 0, 8, 6}},
46+
ans462{14},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 462------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans462, q.para462
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, minMoves2(p.nums))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [462. Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)
2+
3+
4+
## 题目
5+
6+
Given an integer array `nums` of size `n`, return *the minimum number of moves required to make all array elements equal*.
7+
8+
In one move, you can increment or decrement an element of the array by `1`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [1,2,3]
14+
Output: 2
15+
Explanation:
16+
Only two moves are needed (remember each move increments or decrements one element):
17+
[1,2,3] => [2,2,3] => [2,2,2]
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: nums = [1,10,2,9]
24+
Output: 16
25+
```
26+
27+
**Constraints:**
28+
29+
- `n == nums.length`
30+
- `1 <= nums.length <= 10^5`
31+
- `109 <= nums[i] <= 10^9`
32+
33+
## 题目大意
34+
35+
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加 1 或减 1。 您可以假设数组的长度最多为10000。
36+
37+
## 解题思路
38+
39+
- 这题抽象成数学问题是,如果我们把数组 a 中的每个数看成水平轴上的一个点,那么根据上面的移动次数公式,我们需要找到在水平轴上找到一个点 x,使得这 N 个点到 x 的距离之和最小。有 2 个点值得我们考虑,一个是中位数,另外一个是平均值。举个简单的例子,[1,0,0,8,6] 这组数据,中位数是 1,平均值是 3 。分别计算移动的步数,按照中位数对齐是 14,按照平均值对齐是 16 。所以选择中位数。
40+
- 此题可以用数学证明,证明出,按照平均值移动的步数 ≥ 按照中位数移动的步数。具体证明笔者这里不证明了,感兴趣的同学可以自己证明试试。
41+
42+
## 代码
43+
44+
```go
45+
package leetcode
46+
47+
import (
48+
"math"
49+
"sort"
50+
)
51+
52+
func minMoves2(nums []int) int {
53+
if len(nums) == 0 {
54+
return 0
55+
}
56+
moves, mid := 0, len(nums)/2
57+
sort.Ints(nums)
58+
for i := range nums {
59+
if i == mid {
60+
continue
61+
}
62+
moves += int(math.Abs(float64(nums[mid] - nums[i])))
63+
}
64+
return moves
65+
}
66+
```

website/content/ChapterFour/0400~0499/0454.4Sum-II.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Explanation:
2323
The two tuples are:
2424
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2525
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
26-
3.
26+
2727
```
2828

2929

website/content/ChapterFour/0400~0499/0461.Hamming-Distance.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ func hammingDistance(x int, y int) int {
5656
----------------------------------------------
5757
<div style="display: flex;justify-content: space-between;align-items: center;">
5858
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0460.LFU-Cache/">⬅️上一页</a></p>
59-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0463.Island-Perimeter/">下一页➡️</a></p>
59+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0462.Minimum-Moves-to-Equal-Array-Elements-II/">下一页➡️</a></p>
6060
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [462. Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/)
2+
3+
4+
## 题目
5+
6+
Given an integer array `nums` of size `n`, return *the minimum number of moves required to make all array elements equal*.
7+
8+
In one move, you can increment or decrement an element of the array by `1`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [1,2,3]
14+
Output: 2
15+
Explanation:
16+
Only two moves are needed (remember each move increments or decrements one element):
17+
[1,2,3] => [2,2,3] => [2,2,2]
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: nums = [1,10,2,9]
24+
Output: 16
25+
```
26+
27+
**Constraints:**
28+
29+
- `n == nums.length`
30+
- `1 <= nums.length <= 10^5`
31+
- `109 <= nums[i] <= 10^9`
32+
33+
## 题目大意
34+
35+
给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加 1 或减 1。 您可以假设数组的长度最多为10000。
36+
37+
## 解题思路
38+
39+
- 这题抽象成数学问题是,如果我们把数组 a 中的每个数看成水平轴上的一个点,那么根据上面的移动次数公式,我们需要找到在水平轴上找到一个点 x,使得这 N 个点到 x 的距离之和最小。有 2 个点值得我们考虑,一个是中位数,另外一个是平均值。举个简单的例子,[1,0,0,8,6] 这组数据,中位数是 1,平均值是 3 。分别计算移动的步数,按照中位数对齐是 14,按照平均值对齐是 16 。所以选择中位数。
40+
- 此题可以用数学证明,证明出,按照平均值移动的步数 ≥ 按照中位数移动的步数。具体证明笔者这里不证明了,感兴趣的同学可以自己证明试试。
41+
42+
## 代码
43+
44+
```go
45+
package leetcode
46+
47+
import (
48+
"math"
49+
"sort"
50+
)
51+
52+
func minMoves2(nums []int) int {
53+
if len(nums) == 0 {
54+
return 0
55+
}
56+
moves, mid := 0, len(nums)/2
57+
sort.Ints(nums)
58+
for i := range nums {
59+
if i == mid {
60+
continue
61+
}
62+
moves += int(math.Abs(float64(nums[mid] - nums[i])))
63+
}
64+
return moves
65+
}
66+
```
67+
68+
69+
----------------------------------------------
70+
<div style="display: flex;justify-content: space-between;align-items: center;">
71+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0461.Hamming-Distance/">⬅️上一页</a></p>
72+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0463.Island-Perimeter/">下一页➡️</a></p>
73+
</div>

website/content/ChapterFour/0400~0499/0463.Island-Perimeter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ func islandPerimeter(grid [][]int) int {
7373

7474
----------------------------------------------
7575
<div style="display: flex;justify-content: space-between;align-items: center;">
76-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0461.Hamming-Distance/">⬅️上一页</a></p>
76+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0462.Minimum-Moves-to-Equal-Array-Elements-II/">⬅️上一页</a></p>
7777
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0470.Implement-Rand10-Using-Rand7/">下一页➡️</a></p>
7878
</div>

0 commit comments

Comments
 (0)