当前位置:网站首页>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 .
边栏推荐
- Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
- Basic principles of video compression coding and audio compression coding
- 视频压缩编码和音频压缩编码基本原理
- Chapter 5 yarn resource scheduler
- Discussion on QWidget code setting style sheet
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Codeforces Round #799 (Div. 4)A~H
- ffmpeg命令行使用
- useEffect,函数组件挂载和卸载时触发
- 第5章 NameNode和SecondaryNameNode
猜你喜欢
本地可视化工具连接阿里云centOS服务器的redis
Kubernetes集群部署
QT implementation window gradually disappears qpropertyanimation+ progress bar
去掉input聚焦时的边框
Chapter 6 datanode
Kubernetes cluster deployment
Hbuilder X格式化快捷键设置
Detailed explanation of FLV format
Simple records of business system migration from Oracle to opengauss database
sublime text 代码格式化操作
随机推荐
MariaDB的安装与配置
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
Solve the problem that intel12 generation core CPU single thread only runs on small cores
Base dice (dynamic programming + matrix fast power)
Codeforces Round #771 (Div. 2)
JS time function Daquan detailed explanation ----- AHAO blog
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Browser print margin, default / borderless, full 1 page A4
Study notes of Tutu - process
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Li Kou - 298th weekly match
第2章 HFDS的Shell操作
Calculate the time difference
Chapter 5 namenode and secondarynamenode
CMake速成
js时间函数大全 详细的讲解 -----阿浩博客
<li>圆点样式 list-style-type
Input can only input numbers, limited input
Codeforces Global Round 19