Skip to content

Commit 39360ae

Browse files
authored
Merge pull request halfrost#228 from gostool/leetcode1984
Leetcode1984
2 parents a8993ba + c1b9dae commit 39360ae

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func minimumDifference(nums []int, k int) int {
6+
sort.Ints(nums)
7+
minDiff := 100000 + 1
8+
for i := 0; i < len(nums); i++ {
9+
if i+k-1 >= len(nums) {
10+
break
11+
}
12+
diff := nums[i+k-1] - nums[i]
13+
if diff < minDiff {
14+
minDiff = diff
15+
}
16+
}
17+
return minDiff
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1984 struct {
9+
para1984
10+
ans1984
11+
}
12+
13+
// para 是参数
14+
type para1984 struct {
15+
nums []int
16+
k int
17+
}
18+
19+
// ans 是答案
20+
type ans1984 struct {
21+
ans int
22+
}
23+
24+
func Test_Problem1984(t *testing.T) {
25+
26+
qs := []question1984{
27+
28+
{
29+
para1984{[]int{90}, 1},
30+
ans1984{0},
31+
},
32+
33+
{
34+
para1984{[]int{9, 4, 1, 7}, 2},
35+
ans1984{2},
36+
},
37+
}
38+
39+
fmt.Printf("------------------------Leetcode Problem 1984------------------------\n")
40+
41+
for _, q := range qs {
42+
_, p := q.ans1984, q.para1984
43+
fmt.Printf("【input】:%v ", p)
44+
fmt.Printf("【output】:%v \n", minimumDifference(p.nums, p.k))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [1984. Minimum Difference Between Highest and Lowest of K Scores](https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/)
2+
3+
## 题目
4+
5+
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.
6+
7+
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.
8+
9+
Return the minimum possible difference.
10+
11+
**Example 1:**
12+
13+
Input: nums = [90], k = 1
14+
Output: 0
15+
Explanation: There is one way to pick score(s) of one student:
16+
- [90]. The difference between the highest and lowest score is 90 - 90 = 0.
17+
The minimum possible difference is 0.
18+
19+
**Example 2:**
20+
21+
Input: nums = [9,4,1,7], k = 2
22+
Output: 2
23+
Explanation: There are six ways to pick score(s) of two students:
24+
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
25+
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
26+
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
27+
- [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
28+
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
29+
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
30+
The minimum possible difference is 2.
31+
32+
**Constraints:**
33+
34+
- 1 <= k <= nums.length <= 1000
35+
- 0 <= nums[i] <= 100000
36+
37+
## 题目大意
38+
39+
给你一个下标从 0 开始的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。
40+
41+
从数组中选出任意 k 名学生的分数,使这 k 个分数间最高分和最低分的差值达到最小化 。
42+
43+
返回可能的最小差值 。
44+
45+
## 解题思路
46+
47+
- nums 排序
48+
- 求出nums[i+k-1] - nums[i]中的最小差值
49+
50+
## 代码
51+
52+
```go
53+
package leetcode
54+
55+
import "sort"
56+
57+
func minimumDifference(nums []int, k int) int {
58+
sort.Ints(nums)
59+
minDiff := 100000 + 1
60+
for i := 0; i < len(nums); i++ {
61+
if i+k-1 >= len(nums) {
62+
break
63+
}
64+
diff := nums[i+k-1] - nums[i]
65+
if diff < minDiff {
66+
minDiff = diff
67+
}
68+
}
69+
return minDiff
70+
}
71+
```

0 commit comments

Comments
 (0)