Skip to content

Add solution and test-cases for problem 1509 #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
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
@@ -1,28 +1,48 @@
# [1509.Minimum Difference Between Largest and Smallest Value in Three Moves][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given an integer array `nums`.

In one move, you can choose one element of `nums` and change it to **any value**.

Return the minimum difference between the largest and smallest value of `nums` **after performing at most three moves**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [5,3,2,4]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 2 to 3. nums becomes [5,3,3,4].
In the second move, change 4 to 3. nums becomes [5,3,3,3].
In the third move, change 5 to 3. nums becomes [3,3,3,3].
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Minimum Difference Between Largest and Smallest Value in Three Moves
```go
```
Input: nums = [1,5,0,10,14]
Output: 1
Explanation: We can make at most 3 moves.
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
It can be shown that there is no way to make the difference 0 in 3 moves.
```

**Example 3:**

```
Input: nums = [3,100,20]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 100 to 7. nums becomes [3,7,20].
In the second move, change 20 to 7. nums becomes [3,7,7].
In the third move, change 3 to 7. nums becomes [7,7,7].
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
import (
"math"
"sort"
)

func Solution(nums []int) int {
l := len(nums)
if l <= 4 {
return 0
}
sort.Ints(nums)
// 如果最后元素全都变了,那就是nums[l-4]-num[0]了
// 如果不是处理最后三个元素而至最后两个,然后把最小的给干掉,将最小的数值提升,然后看
// 0,1,1, 4, 6, 6, 6, 6
// 如果处理最后三个最大的就是nums[l-4], 然后是nums[l-3], ...
// 如果处理后面两个,那就是提升最小的值,
// ... 如果最后面的数据一个都不处理,那就是直接提升最小值
left := 0
ans := math.MaxInt
for i := l - 4; i < l; i++ {
ans = min(ans, nums[i]-nums[left])
left++
}

return ans
//return nums[len(nums)-4] - nums[0]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{5, 3, 2, 4}, 0},
{"TestCase2", []int{1, 5, 0, 10, 14}, 1},
{"TestCase3", []int{3, 100, 20}, 0},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading