当前位置:网站首页>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;
}
}
边栏推荐
- U++4 接口 学习笔记
- You can't sell the used lithography machine to China! The United States unreasonably pressured the Dutch ASML, and domestic chips were suppressed again
- Canteen user dish relationship system (C language course design)
- Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
- 九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
- [hand torn STL] list
- Section 1: (3) logic chip process substrate selection
- 如何设计 API 接口,实现统一格式返回?
- U++ 游戏类 学习笔记
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
猜你喜欢
C语言中函数指针与指针函数
【736. Lisp 语法解析】
MySQL数据库(基础篇)
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Flex layout and usage
A line of R code draws the population pyramid
Vscode automatically adds a semicolon and jumps to the next line
System framework of PureMVC
Depth first traversal template principle of tree and graph
A row of code r shows the table of Cox regression model
随机推荐
什么是Web3
【愚公系列】2022年7月 Go教学课程 005-变量
树与图的深度优先遍历模版原理
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
Jetson nano configures pytorch deep learning environment / / to be improved
Meaning of 'n:m' and '1:n' in database design
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
组织实战攻防演练的5个阶段
使用Thread类和Runnable接口实现多线程的区别
U++4 接口 学习笔记
How does vscade use the built-in browser?
指针与数组在函数中输入实现逆序输出
Appium practice | make the test faster, more stable and more reliable (I): slice test
The most complete learning rate adjustment strategy in history LR_ scheduler
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
STM32 system timer flashing LED
File upload vulnerability summary
U++ 游戏类 学习笔记
Some understandings about 01 backpacker
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name