当前位置:网站首页>【LeetCode】1984. Minimum difference between highest and lowest of K scores
【LeetCode】1984. Minimum difference between highest and lowest of K scores
2022-06-26 22:11:00 【Negative snow candle】
- author : A bright candle with snow
- id: fuxuemingzhu
- Personal blog : http://fuxuemingzhu.cn/
- official account : A bright candle with snow
- Key words of this article :Leetcode, Power button , Brush problem ,Python, C++, Array , The sliding window , Difference value
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 .
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 <= 10000 <= nums[i] <= 10^5
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores
The main idea of the topic
Pick anything from the array k A digital , Let this k A number of Maximum - minimum value The smallest result of .
How to solve the problem
With nums = [9,4,1,7], k = 2 For example , We see when choosing [9, 7] When these two numbers , The difference between the two figures is 2, This difference is the smallest . No matter which other two numbers you choose , Difference ratio 2 Big .
How to find k The difference between the maximum value and the minimum value of a number ? It reminds us of , Sort the array , Then use a size of k Sliding window to traverse the entire array . The rightmost value of the sliding window is the maximum value in the window , The leftmost value of the sliding window is the minimum value in the window .
therefore , What we are looking for is an array that has been sorted , All sizes are k In the sliding window of , The rightmost digit - Leftmost digit Minimum result of .

C++ The code is as follows :
class Solution {
public:
int minimumDifference(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int res = INT_MAX;
for (int i = 0; i <= nums.size() - k; ++i) {
res = min(res, nums[i + k - 1] - nums[i]);
}
return res;
}
};
- Time complexity : O ( N ∗ l o g ( N ) ) O(N*log(N)) O(N∗log(N)),N Is the length of the array .
- Spatial complexity : O ( 1 ) O(1) O(1).
summary
- about Easy subject , It is generally easy to write . The emphasis is on the abstraction of the topic .
- Notice the border of the sliding window , Do not exceed the range of the array .
I am a @ A bright candle with snow , Brush algorithm questions 1000 multichannel , Yes 1000 Several algorithm solutions , Harvest reading 300 ten thousand . Pay attention to me , You won't miss my wonderful animation solution 、 Share interview questions 、 Team activity , Enter home page @ A bright candle with snow There are brush questions on the right , From then on, I'm no longer alone .
- When I brush the questions , If you don't know how to brush the questions , You can see LeetCode How should I brush ?
- If you think there are too many problems , Want to improve quickly in a short time , You can see LeetCode The most classic 100 Problem .
- Send you a code template to brush the questions :【LeetCode】 The code template , Brush questions will
date
2022 year 2 month 11 Japan —— I just went to Sanya for a trip in the new year
边栏推荐
- Weaving dream collection plug-ins are recommended to be free collection plug-ins
- AI智能抠图工具--头发丝都可见
- leetcode:710. Random numbers in the blacklist [mapping thinking]
- 中金财富开户安全吗?我想开户炒股。
- 【混合编程jni 】第六篇之native 中字符串和数组的操作
- 证券注册开户有没有什么风险?安全吗?
- Is there any risk for flush to register and open an account? Is it safe?
- 【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
- Can compass open an account for stock trading? Is it safe?
- 矩阵求导及其链式法则
猜你喜欢

树莓派初步使用

Configuring assimp Library in QT environment (MinGW compiler)

AI智能抠图工具--头发丝都可见

Vulnhub's DC8

大龄程序员的一些出路

Product design in the extreme Internet Era

DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability

Weaving dream collection plug-ins are recommended to be free collection plug-ins

在Flutter中解析复杂的JSON

About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
随机推荐
【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
Flutter 中 ValueNotifier<List<T>> 监听问题解决
CVPR 2022 | 美团技术团队精选论文解读
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
JupyterLab 常用配置
指南针能开户炒股吗?安全吗?
FPGA -vga display
Convolutional neural network (CNN) explanation and tensorflow2 code implementation
在哪个平台买股票开户最安全?求分享
VB. Net class library - 4 screen shots, clipping
How to enable Hana cloud service on SAP BTP platform
Solution of valuenotifier < list < t > > monitoring problem in fluent
CVPR 2022 - Interpretation of selected papers of meituan technical team
Introduction to operator
【BUG反馈】WebIM在线聊天系统发消息时间问题
数据治理啥都干
同花顺注册开户有没有什么风险?安全吗?
Centos7编译安装Redis
Some ways out for older programmers
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process