当前位置:网站首页>LeetCode 1984. Minimum difference in student scores
LeetCode 1984. Minimum difference in student scores
2022-07-06 16:43:00 【Daylight629】
1984. The minimum difference in student scores
To give you one Subscript from 0 Start Array of integers for nums
, among nums[i]
It means the first one i
A student's grade . I'll give you another integer k
.
Select any... From the array k
A student's grade , Make this k
Between scores The highest and Lowest score Of Difference value achieve To minimize the .
Return possible Minimum difference .
Example 1:
Input :nums = [90], k = 1
Output :0
explain : elect 1 A student's grade , have only 1 Methods :
- [90] The difference between the highest score and the lowest score is 90 - 90 = 0
The smallest possible difference is 0
Example 2:
Input :nums = [9,4,1,7], k = 2
Output :2
explain : elect 2 A student's grade , Yes 6 Methods :
- [9,4,1,7] The difference between the highest score and the lowest score is 9 - 4 = 5
- [9,4,1,7] The difference between the highest score and the lowest score is 9 - 1 = 8
- [9,4,1,7] The difference between the highest score and the lowest score is 9 - 7 = 2
- [9,4,1,7] The difference between the highest score and the lowest score is 4 - 1 = 3
- [9,4,1,7] The difference between the highest score and the lowest score is 7 - 4 = 3
- [9,4,1,7] The difference between the highest score and the lowest score is 7 - 1 = 6
The smallest possible difference is 2
Tips :
1 <= k <= nums.length <= 1000
0 <= nums[i] <= 105
Two 、 Method 1
Sort
class Solution {
public int minimumDifference(int[] nums, int k) {
int res = Integer.MAX_VALUE;
Arrays.sort(nums);
for (int i = 0; i + k - 1 < nums.length; i++) {
res = Math.min(res, nums[k + i - 1] - nums[i]);
}
return res;
}
}
Complexity analysis
Time complexity :O(nlogn), among n It's an array nums The length of . The time required for sorting is O(nlogn), The time required for subsequent traversal is O(n).
Spatial complexity :O(logn), This is the stack space needed for sorting .
边栏推荐
- MariaDB的安装与配置
- 第6章 Rebalance详解
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- It is forbidden to trigger onchange in antd upload beforeupload
- 业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
- Remove the border when input is focused
- 第五章 Yarn资源调度器
- 浏览器打印边距,默认/无边距,占满1页A4
猜你喜欢
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
第5章 NameNode和SecondaryNameNode
Simply try the new amp model of deepfacelab (deepfake)
顺丰科技智慧物流校园技术挑战赛(无t4)
Discussion on QWidget code setting style sheet
Chapter 2 shell operation of hfds
Codeforces round 797 (Div. 3) no f
antd upload beforeUpload中禁止触发onchange
CMake速成
Chapter 5 detailed explanation of consumer groups
随机推荐
Codeforces Round #800 (Div. 2)AC
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
第五章 Yarn资源调度器
第6章 Rebalance详解
业务系统从Oracle迁移到openGauss数据库的简单记录
Li Kou - 298th weekly match
Acwing - game 55 of the week
Codeforces Global Round 19
Oneforall installation and use
Spark independent cluster dynamic online and offline worker node
音视频开发面试题
指定格式时间,月份天数前补零
Discussion on QWidget code setting style sheet
Research Report on market supply and demand and strategy of China's four seasons tent industry
Install Jupiter notebook under Anaconda
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Generate random password / verification code