当前位置:网站首页>Leetcode minimum difference in student scores
Leetcode minimum difference in student scores
2022-07-07 05:00:00 【kt1776133839】
Title Description :
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 .
Examples :
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
Java Program :
class Solution {
public int minimumDifference(int[] nums, int k) {
Arrays.sort(nums);
int ans=nums[k-1]-nums[0];
for(int i=1;i<=nums.length-k;i++){ans=Math.min(ans,nums[i+k-1]-nums[i]);}
return ans;
}
}
边栏推荐
- PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
- 3.基金的类型
- JS variable
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- Jetson nano配置pytorch深度学习环境//待完善
- Meaning of 'n:m' and '1:n' in database design
- Introduction to the PureMVC series
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
- Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
- Decorator basic learning 02
猜你喜欢
为什么很多人对技术债务产生误解
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
Field data acquisition and edge calculation scheme of CNC machine tools
Lessons and thoughts of the first SQL injection
Oracle -- 视图与序列
C语言中函数指针与指针函数
01机器学习相关规定
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
JS variable plus
窗口可不是什么便宜的东西
随机推荐
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
Basic knowledge of road loss of 3GPP channel model
Two methods of chromosome coordinate sequencing
STM32F103 realize IAP online upgrade application
01 machine learning related regulations
[digital analog] source code of MATLAB allcycles() function (not available before 2021a)
Organize five stages of actual attack and defense drill
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
JS variable plus
程序员上班摸鱼,这么玩才高端!
acwing 843. n-皇后问题
Lessons and thoughts of the first SQL injection
leetcode 53. Maximum Subarray 最大子数组和(中等)
Introduction to the PureMVC series
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Section 1: (3) logic chip process substrate selection
树与图的深度优先遍历模版原理
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Decorator basic learning 02
How to package the parsed Excel data into objects and write this object set into the database?