当前位置:网站首页>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;
}
边栏推荐
- CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
- Open3D ISS关键点
- MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
- Virtual address space
- 解析机器人科技发展观对社会研究论
- Thirteen forms of lambda in kotlin
- Understanding of out covariance, in inversion and invariance in kotlin
- The single value view in Splunk uses to replace numeric values with text
- POJ - 3784 Running Median(对顶堆)
- Opencv learning note 4 - expansion / corrosion / open operation / close operation
猜你喜欢
Using helm to install rainbow in various kubernetes
Rainbow combines neuvector to practice container safety management
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
国标GB28181协议视频平台EasyGBS新增拉流超时配置
下载和安装orcale database11.2.0.4
Data type - floating point (C language)
opencv学习笔记一——读取图像的几种方法
饥荒云服管理脚本
2-3查找樹
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
随机推荐
Give full play to the wide practicality of maker education space
AVL平衡二叉搜索树
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
[kuangbin]专题十五 数位DP
BiSeNet的特点
使用BiSeNet实现自己的数据集
IELTS review progress and method use [daily revision]
Bisenet features
POJ - 3784 Running Median(对顶堆)
饥荒云服管理脚本
Lua programming learning notes
Splunk query CSV lookup table data dynamic query
opencv学习笔记三——图像平滑/去噪处理
Le système mes est un choix nécessaire pour la production de l'entreprise
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
Opencv learning note 3 - image smoothing / denoising
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
XCiT学习笔记
Understanding of out covariance, in inversion and invariance in kotlin
Interface as a parameter (interface callback)