Skip to content

Commit f999c2b

Browse files
committed
chore: update 1984. Minimum Difference Between Highest and Lowest of K Scores
1 parent bffd38c commit f999c2b

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 1984. Minimum Difference Between Highest and Lowest of K Scores
3+
description: 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.
4+
keywords:
5+
[
6+
LeetCode,
7+
1984. Minimum Difference Between Highest and Lowest of K Scores,
8+
Easy,
9+
Array,
10+
Sliding Window,
11+
Sorting,
12+
]
13+
tags: [LeetCode, Easy, Array, Sliding Window, Sorting]
14+
---
15+
16+
# [{frontMatter.title}](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/)
17+
18+
import Tabs from "@theme/Tabs";
19+
import TabItem from "@theme/TabItem";
20+
import CodeBlock from "@theme/CodeBlock";
21+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
22+
import Solution from "!!raw-loader!./solution.js";
23+
import Description from "./_Description.md";
24+
import Examples from "./_Examples.md";
25+
26+
<DifficultyBadge difficulty="Easy" />
27+
28+
<Tabs>
29+
<TabItem value="description" label="題目描述" default>
30+
<details open>
31+
<summary>Description</summary>
32+
<Description />
33+
<Examples />
34+
</details>
35+
</TabItem>
36+
37+
<TabItem value="solution" label="解答">
38+
## Solution
39+
<CodeBlock language="js">{Solution}</CodeBlock>
40+
</TabItem>
41+
</Tabs>
42+
43+
## 解題思路
44+
45+
根據題目的 `hints``topics` 得知要將題目給的 `nums` 排序後,使用 `Sliding Window` 的方式來解題,而 `Sliding Window` 的大小是由 `k` 來決定的。
46+
47+
```js
48+
var minimumDifference = function (nums, k) {
49+
nums.sort((a, b) => b - a); // 由大排到小,這樣子 left 就會大於 right
50+
51+
let left = 0;
52+
let right = k - 1;
53+
let minimumDifference = Infinity; // 因為是找minimum所以初始值無限大
54+
55+
while (right < nums.length) {
56+
// 當 sliding window 還沒結束時後就持續算 difference,並每次更新 minimumDifference
57+
const difference = nums[left] - nums[right];
58+
minimumDifference = Math.min(minimumDifference, difference);
59+
left++;
60+
right++; // 算完之後 sliding window 往右移動
61+
}
62+
63+
return minimumDifference; // while迴圈結束後回傳結果
64+
};
65+
```
66+
67+
## 心得
68+
69+
一開始覺得看不太懂題目但竟然一次就過ㄌ耶!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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.
2+
3+
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.
4+
5+
Return the minimum possible difference.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [90], k = 1
4+
> **Output:** 0
5+
> **Explanation:**
6+
> There is one way to pick score(s) of one student:
7+
> -[90]. The difference between the highest and lowest score is 90 - 90 = 0.
8+
> The minimum possible difference is 0.
9+
10+
#### Example 2:
11+
12+
> **Input:** nums = [9,4,1,7], k = 2
13+
> **Output:** 2
14+
> **Explanation:**
15+
> There are six ways to pick score(s) of two students:
16+
>
17+
> - [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
18+
> - [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
19+
> - [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
20+
> - [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
21+
> - [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
22+
> - [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
23+
> The minimum possible difference is 2.
24+
25+
## Constraints:
26+
27+
- `1 <= k <= nums.length <= 1000`
28+
- `0` <= `nums[i]` <= $10^5$
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var minimumDifference = function (nums, k) {
7+
nums.sort((a, b) => b - a);
8+
9+
let left = 0;
10+
let right = k - 1;
11+
let minimumDifference = Infinity;
12+
13+
while (right < nums.length) {
14+
const difference = nums[left] - nums[right];
15+
minimumDifference = Math.min(minimumDifference, difference);
16+
left++;
17+
right++;
18+
}
19+
20+
return minimumDifference;
21+
};

0 commit comments

Comments
 (0)