当前位置:网站首页>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 .
边栏推荐
- Tree of life (tree DP)
- QT implementation fillet window
- Bisphenol based CE Resin Industry Research Report - market status analysis and development prospect forecast
- Codeforces Round #800 (Div. 2)AC
- sublime text 代码格式化操作
- 第7章 __consumer_offsets topic
- Calculate the time difference
- CMake Error: Could not create named generator Visual Studio 16 2019解决方法
- Install Jupiter notebook under Anaconda
- Acwing: Game 58 of the week
猜你喜欢
Install Jupiter notebook under Anaconda
Remove the border when input is focused
解决Intel12代酷睿CPU单线程只给小核运行的问题
图像处理一百题(1-10)
Local visualization tools are connected to redis of Alibaba cloud CentOS server
第2章 HFDS的Shell操作
ByteDance new programmer's growth secret: those glittering treasures mentors
Spark独立集群动态上线下线Worker节点
Discussion on QWidget code setting style sheet
新手必会的静态站点生成器——Gridsome
随机推荐
Spark independent cluster dynamic online and offline worker node
顺丰科技智慧物流校园技术挑战赛(无t4)
Log statistics (double pointer)
力扣leetcode第 280 场周赛
解决Intel12代酷睿CPU单线程调度问题(二)
浏览器打印边距,默认/无边距,占满1页A4
The concept of spark independent cluster worker and executor
去掉input聚焦时的边框
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
How to insert mathematical formulas in CSDN blog
解决Intel12代酷睿CPU单线程只给小核运行的问题
Codeforces - 1526C1&&C2 - Potions
Click QT button to switch qlineedit focus (including code)
QT realizes window topping, topping state switching, and multi window topping priority relationship
本地可视化工具连接阿里云centOS服务器的redis
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
腾讯面试算法题
Discussion on QWidget code setting style sheet
QT simulates mouse events and realizes clicking, double clicking, moving and dragging