当前位置:网站首页>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;
}
}
边栏推荐
- 3.基金的类型
- 【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
- Acl2022 | decomposed meta learning small sample named entity recognition
- 日常工作中程序员最讨厌哪些工作事项?
- 2.证券投资基金的概述
- Camera calibration (I): robot hand eye calibration
- 树与图的深度优先遍历模版原理
- Markdown editor
- What work items do programmers hate most in their daily work?
- Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
猜你喜欢
Monitoring cannot be started after Oracle modifies the computer name
指针与数组在函数中输入实现逆序输出
Error: No named parameter with the name ‘foregroundColor‘
Oracle -- 视图与序列
为什么很多人对技术债务产生误解
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
U++ 游戏类 学习笔记
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
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
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
随机推荐
2.证券投资基金的概述
Structure actual training camp | after class homework | module 6
leetcode 53. Maximum Subarray 最大子数组和(中等)
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
If you ask me about R code debugging, I will tell you head, STR, help
JS variable plus
Markdown编辑器
一文搞懂常见的网络I/O模型
STM32F103实现IAP在线升级应用程序
3.基金的类型
namespace基础介绍
Organize five stages of actual attack and defense drill
抖音或将推出独立种草社区平台:会不会成为第二个小红书
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
STM32F103 realize IAP online upgrade application
深入解析Kubebuilder
Servicemesh mainly solves three pain points
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)
Run the command once per second in Bash- Run command every second in Bash?
组织实战攻防演练的5个阶段