当前位置:网站首页>Leetcode 1984. Minimum difference in student scores
Leetcode 1984. Minimum difference in student scores
2022-07-07 08:32:00 【Brother. soup】
subject
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
Ideas
1. To find the minimum difference , Then the first thought should be sorting , We can use Arrays.sort Methods , Implement non descending arrangement .
2. After arranging the order , We according to the k value , Compare the differences in turn k The size of adjacent values of units .
Code
public static int minimumDifference(int [] nums,int k){
Arrays.sort(nums);
int ans=nums[k-1]-nums[0];
for(int i=k;i<nums.length;i++){
ans=Math.min(ans,nums[i]-nums[i-k+1]);
}
return ans;
}
边栏推荐
- GFS distributed file system
- Lua 编程学习笔记
- Several ways of lambda used in functions in kotlin (higher-order functions)
- A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
- Opencv learning note 3 - image smoothing / denoising
- BiSeNet的特点
- 【雅思口语】安娜口语学习记录 Part2
- Golang compilation constraint / conditional compilation (/ / +build < tags>)
- One click installation of highly available Nacos clusters in rainbow
- [IELTS speaking] Anna's oral learning records Part3
猜你喜欢
rsync远程同步
opencv学习笔记三——图像平滑/去噪处理
opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
饥荒云服管理脚本
Opencv learning notes 1 -- several methods of reading images
在Rainbond中实现数据库结构自动化升级
2-3 lookup tree
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Pvtv2--pyramid vision transformer V2 learning notes
利用 Helm 在各类 Kubernetes 中安装 Rainbond
随机推荐
Xcit learning notes
Data type - floating point (C language)
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Analyzing the influence of robot science and technology development concept on Social Research
opencv学习笔记一——读取图像的几种方法
[hard core science popularization] working principle of dynamic loop monitoring system
[IELTS speaking] Anna's oral learning records Part3
Obsidan之数学公式的输入
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Pytoch (VI) -- model tuning tricks
JEditableTable的使用技巧
Function extension, attribute extension and non empty type extension in kotlin
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
GOLand idea intellij 无法输入汉字
雅思考试自己的复习进度以及方法使用【日更版】
Interpreting the practical application of maker thinking and mathematics curriculum
Tuowei information uses the cloud native landing practice of rainbow
Pvtv2--pyramid vision transformer V2 learning notes
The field value in Splunk subquery fuzzy matching CSV is*
Ebpf cilium practice (1) - team based network isolation