Skip to content

Commit 004a9e6

Browse files
authored
Merge pull request #923 from 0xff-dev/1509
Add solution and test-cases for problem 1509
2 parents be90169 + 1af027e commit 004a9e6

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [1509.Minimum Difference Between Largest and Smallest Value in Three Moves][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`.
5+
6+
In one move, you can choose one element of `nums` and change it to **any value**.
7+
8+
Return the minimum difference between the largest and smallest value of `nums` **after performing at most three moves**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [5,3,2,4]
14+
Output: 0
15+
Explanation: We can make at most 3 moves.
16+
In the first move, change 2 to 3. nums becomes [5,3,3,4].
17+
In the second move, change 4 to 3. nums becomes [5,3,3,3].
18+
In the third move, change 5 to 3. nums becomes [3,3,3,3].
19+
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
1320
```
1421

15-
## 题意
16-
> ...
17-
18-
## 题解
22+
**Example 2:**
1923

20-
### 思路1
21-
> ...
22-
Minimum Difference Between Largest and Smallest Value in Three Moves
23-
```go
2424
```
25+
Input: nums = [1,5,0,10,14]
26+
Output: 1
27+
Explanation: We can make at most 3 moves.
28+
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
29+
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
30+
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
31+
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
32+
It can be shown that there is no way to make the difference 0 in 3 moves.
33+
```
34+
35+
**Example 3:**
2536

37+
```
38+
Input: nums = [3,100,20]
39+
Output: 0
40+
Explanation: We can make at most 3 moves.
41+
In the first move, change 100 to 7. nums becomes [3,7,20].
42+
In the second move, change 20 to 7. nums becomes [3,7,7].
43+
In the third move, change 3 to 7. nums becomes [7,7,7].
44+
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
45+
```
2646

2747
## 结语
2848

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func Solution(nums []int) int {
9+
l := len(nums)
10+
if l <= 4 {
11+
return 0
12+
}
13+
sort.Ints(nums)
14+
// 如果最后元素全都变了,那就是nums[l-4]-num[0]了
15+
// 如果不是处理最后三个元素而至最后两个,然后把最小的给干掉,将最小的数值提升,然后看
16+
// 0,1,1, 4, 6, 6, 6, 6
17+
// 如果处理最后三个最大的就是nums[l-4], 然后是nums[l-3], ...
18+
// 如果处理后面两个,那就是提升最小的值,
19+
// ... 如果最后面的数据一个都不处理,那就是直接提升最小值
20+
left := 0
21+
ans := math.MaxInt
22+
for i := l - 4; i < l; i++ {
23+
ans = min(ans, nums[i]-nums[left])
24+
left++
25+
}
26+
27+
return ans
28+
//return nums[len(nums)-4] - nums[0]
529
}

leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{5, 3, 2, 4}, 0},
17+
{"TestCase2", []int{1, 5, 0, 10, 14}, 1},
18+
{"TestCase3", []int{3, 100, 20}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)