当前位置:网站首页>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;
}
边栏推荐
- 接口作为参数(接口回调)
- National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
- [kuangbin] topic 15 digit DP
- Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
- Transformation function map and flatmap in kotlin
- Infix keyword infix expression and the use of generic extension function in kotlin
- [quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
- 基本数据类型和string类型互相转化
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Snyk dependency security vulnerability scanning tool
猜你喜欢
Splunk中single value视图使用将数值替换为文字
SSM integration
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
2-3查找树
opencv学习笔记一——读取图像的几种方法
使用BiSeNet实现自己的数据集
Bisenet features
Pvtv2--pyramid vision transformer V2 learning notes
Explore creativity in steam art design
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
随机推荐
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
The field value in Splunk subquery fuzzy matching CSV is*
单场带货涨粉10万,农村主播竟将男装卖爆单?
[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
SSM 整合
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Several ways of lambda used in functions in kotlin (higher-order functions)
Snyk 依赖性安全漏洞扫描工具
PLSQL的安装和配置
The truth of robot education in hands-on practice
MES系統,是企業生產的必要選擇
Wang Zijian: is the NFT of Tencent magic core worth buying?
Open3d ISS key points
Implement your own dataset using bisenet
Thirteen forms of lambda in kotlin
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
BiSeNet的特点
Low success rate of unit test report
Zcmu--1492: problem d (C language)
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)