Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff97ebf

Browse files
feat: add solutions to lc problem: No.1984 (doocs#605)
No.1984.Minimum Difference Between Highest and Lowest of K Scores
1 parent e3da8b2 commit ff97ebf

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

‎solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,35 @@
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60-
60+
class Solution:
61+
def minimumDifference(self, nums: List[int], k: int) -> int:
62+
if k == 1:
63+
return 0
64+
nums.sort()
65+
ans = maxsize
66+
for i in range(len(nums) - k + 1):
67+
ans = min(ans, nums[i + k - 1] - nums[i])
68+
return ans
6169
```
6270

6371
### **Java**
6472

6573
<!-- 这里可写当前语言的特殊实现逻辑 -->
6674

6775
```java
68-
76+
class Solution {
77+
public int minimumDifference(int[] nums, int k) {
78+
if (k == 1) {
79+
return 0;
80+
}
81+
Arrays.sort(nums);
82+
int min = Integer.MAX_VALUE;
83+
for (int i = 0; i < nums.length - k + 1; i++) {
84+
min = Math.min((nums[i + k - 1] - nums[i]), min);
85+
}
86+
return min;
87+
}
88+
}
6989
```
7090

7191
### **...**

‎solution/1900-1999/1984.Minimum Difference Between Highest and Lowest of K Scores/README_EN.md‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,33 @@ The minimum possible difference is 2.</pre>
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def minimumDifference(self, nums: List[int], k: int) -> int:
56+
if k == 1:
57+
return 0
58+
nums.sort()
59+
ans = maxsize
60+
for i in range(len(nums) - k + 1):
61+
ans = min(ans, nums[i + k - 1] - nums[i])
62+
return ans
5563
```
5664

5765
### **Java**
5866

5967
```java
60-
68+
class Solution {
69+
public int minimumDifference(int[] nums, int k) {
70+
if (k == 1) {
71+
return 0;
72+
}
73+
Arrays.sort(nums);
74+
int min = Integer.MAX_VALUE;
75+
for (int i = 0; i < nums.length - k + 1; i++) {
76+
min = Math.min((nums[i + k - 1] - nums[i]), min);
77+
}
78+
return min;
79+
}
80+
}
6181
```
6282

6383
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minimumDifference(int[] nums, int k) {
3+
if (k == 1) {
4+
return 0;
5+
}
6+
Arrays.sort(nums);
7+
int min = Integer.MAX_VALUE;
8+
for (int i = 0; i < nums.length - k + 1; i++) {
9+
min = Math.min((nums[i + k - 1] - nums[i]), min);
10+
}
11+
return min;
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumDifference(self, nums: List[int], k: int) -> int:
3+
if k == 1:
4+
return 0
5+
nums.sort()
6+
ans = maxsize
7+
for i in range(len(nums) - k + 1):
8+
ans = min(ans, nums[i + k - 1] - nums[i])
9+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /