当前位置:网站首页>Leetcode1984. Minimum difference in student scores
Leetcode1984. Minimum difference in student scores
2022-07-07 22:39:00 【Small size big ha】
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 .
Investigation contents : Sort + The sliding window
The main idea of the topic : Take whatever in the array k It's worth , And then let this k The difference between the maximum value and the minimum value is the smallest .
Ideas : Sort the array first , Think after sorting a[0] To a[k-1] this k It's worth , Suppose we put a[k-1] Replace with array subscript k-1 The number behind ( A number larger than him ), Then the maximum value is determined by a[k-1] Become a larger number , And the minimum value does not change , Then the difference between the maximum value and the minimum value will become larger . So we use this method , from a[0]-a[0+k-1] To a[i]-a[ i+k-1=nums.size() ], Each calculation difference , Record the smallest one as ans.
The attached code :
class Solution {
public:
int minimumDifference(vector<int>& nums, int k) {
int n=nums.size();
sort(nums.begin(),nums.end());
int ans=INT_MAX;
for(int i=0;i+k-1<n;i++)
{
ans=min(ans,nums[i+k-1]-nums[i]);
}
return ans;
}
};
边栏推荐
- ASP.NET Core入门五
- Get the week start time and week end time of the current date
- Kaggle-Titanic
- ASP. Net core introduction V
- 戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
- OpenGL jobs - shaders
- DNS series (I): why does the updated DNS record not take effect?
- 海外代理推荐
- What does it mean to prefix a string with F?
- 如何选择合适的自动化测试工具?
猜你喜欢
UWA Q & a collection
Ni9185 and ni9234 hardware settings in Ni Max
Time standard library
Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
Form组件常用校验规则-2(持续更新中~)
Gazebo import the mapping model created by blender
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
Paint basic graphics with custompaint
operator
随机推荐
Revit secondary development - link file collision detection
C development -- WPF simple animation
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Revit secondary development - cut view
【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
[interview arrangement] 0211 game engine server
How to choose the appropriate automated testing tools?
Write in front -- Talking about program development
Install mxnet GPU version
VTOL in Px4_ att_ Control source code analysis [supplement]
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
What does it mean to prefix a string with F?
php 获取图片信息的方法
operator
Understand the autograd package in pytorch
Aspose. Words merge cells
Form组件常用校验规则-2(持续更新中~)
Cannot find module 'xxx' or its corresponding type declaration
Add get disabled for RC form