From 1af027e00a104ce2c29b7778a1853bcd0d8132e9 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 3 Jul 2024 09:07:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 1509 --- .../README.md | 46 +++++++++++++------ .../Solution.go | 28 ++++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/README.md b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/README.md index f11b69b0a..ab6c8eee1 100755 --- a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/README.md +++ b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution.go b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution.go index d115ccf5e..cc6bf9e84 100644 --- a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution.go +++ b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution.go @@ -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] } diff --git a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution_test.go b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution_test.go index 14ff50eb4..6b4b49664 100644 --- a/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution_test.go +++ b/leetcode/1501-1600/1509.Minimum-Difference-Between-Largest-and-Smallest-Value-in-Three-Moves/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }