当前位置:网站首页>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;
}
};边栏推荐
- Loki, the "open source star picking program", realizes the efficient management of harbor logs
- This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
- 戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
- ASP.NET Core入门五
- How to choose the appropriate automated testing tools?
- The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
- Xcode modifies the default background image of launchscreen and still displays the original image
- ASP. Net core introduction V
- Matplotlib快速入门
- 如何选择合适的自动化测试工具?
猜你喜欢

Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department

Time standard library

Robot autonomous exploration series papers environment code

0-5vac to 4-20mA AC current isolated transmitter / conversion module
![VTOL in Px4_ att_ Control source code analysis [supplement]](/img/7a/4ce0c939b9259faf59c52da2587693.jpg)
VTOL in Px4_ att_ Control source code analysis [supplement]

It's worth seeing. Interview sites and interview skills

. Net automapper use

vite Unrestricted file system access to

Two kinds of updates lost and Solutions
Redis官方ORM框架比RedisTemplate更优雅
随机推荐
Write in front -- Talking about program development
反爬通杀神器
Remember aximp once Use of exe tool
IP network active evaluation system -- x-vision
Aspose. Word operation word document (I)
SAR image quality evaluation
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
OpeGL personal notes - lights
100million single men and women "online dating", supporting 13billion IPOs
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
Robot autonomous exploration DSVP: code parsing
Remove the default background color of chrome input input box
PHP method of obtaining image information
Two methods of calling WCF service by C #
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
Unity local coordinates and world coordinates