Merge pull request #228 from gostool/leetcode1984

Leetcode1984
This commit is contained in:
halfrost
2022-03-01 19:39:36 -08:00
committed by GitHub
3 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package leetcode
import "sort"
func minimumDifference(nums []int, k int) int {
sort.Ints(nums)
minDiff := 100000 + 1
for i := 0; i < len(nums); i++ {
if i+k-1 >= len(nums) {
break
}
diff := nums[i+k-1] - nums[i]
if diff < minDiff {
minDiff = diff
}
}
return minDiff
}

View File

@ -0,0 +1,47 @@
package leetcode
import (
"fmt"
"testing"
)
type question1984 struct {
para1984
ans1984
}
// para 是参数
type para1984 struct {
nums []int
k int
}
// ans 是答案
type ans1984 struct {
ans int
}
func Test_Problem1984(t *testing.T) {
qs := []question1984{
{
para1984{[]int{90}, 1},
ans1984{0},
},
{
para1984{[]int{9, 4, 1, 7}, 2},
ans1984{2},
},
}
fmt.Printf("------------------------Leetcode Problem 1984------------------------\n")
for _, q := range qs {
_, p := q.ans1984, q.para1984
fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", minimumDifference(p.nums, p.k))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,71 @@
# [1984. Minimum Difference Between Highest and Lowest of K Scores](https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/)
## 题目
You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k.
Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k scores is minimized.
Return the minimum possible difference.
**Example 1:**
Input: nums = [90], k = 1
Output: 0
Explanation: There is one way to pick score(s) of one student:
- [90]. The difference between the highest and lowest score is 90 - 90 = 0.
The minimum possible difference is 0.
**Example 2:**
Input: nums = [9,4,1,7], k = 2
Output: 2
Explanation: There are six ways to pick score(s) of two students:
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
- [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
The minimum possible difference is 2.
**Constraints:**
- 1 <= k <= nums.length <= 1000
- 0 <= nums[i] <= 100000
## 题目大意
给你一个下标从 0 开始的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。
从数组中选出任意 k 名学生的分数,使这 k 个分数间最高分和最低分的差值达到最小化 。
返回可能的最小差值 。
## 解题思路
- nums 排序
- 求出nums[i+k-1] - nums[i]中的最小差值
## 代码
```go
package leetcode
import "sort"
func minimumDifference(nums []int, k int) int {
sort.Ints(nums)
minDiff := 100000 + 1
for i := 0; i < len(nums); i++ {
if i+k-1 >= len(nums) {
break
}
diff := nums[i+k-1] - nums[i]
if diff < minDiff {
minDiff = diff
}
}
return minDiff
}
```