当前位置:网站首页>【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
边栏推荐
- Is there any risk in opening a securities registration account? Is it safe?
- [mixed programming JNI] Part 7: JNI command lines
- curl: (35) LibreSSL SSL_ connect: SSL_ ERROR_ SYSCALL in connection
- Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,
- How SAP Spartacus default routing configuration works
- Introduction to dependency injection in SAP Spartacus
- 经典Wide & Deep模型介绍及tensorflow 2代码实现
- 卷积神经网络(CNN)详解及TensorFlow2代码实现
- leetcode - 买卖股票的最佳时机
- Unity 设置Material、Shader的方法
猜你喜欢

Product design in the extreme Internet Era

FPGA -vga display

In 2022, where will the medium and light-weight games go?

Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN

leetcode:152. 乘积最大子数组【考虑两个维度的dp】

Parsing complex JSON in fluent

AI intelligent matting tool - hair can be seen

Pass note 【 dynamic planning 】

Briefly describe the model animation function of unity
![[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]](/img/e8/6342f2dc6e7f06a847852ce4b40719.jpg)
[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
随机推荐
JupyterLab 常用配置
Is there any risk in opening a securities registration account? Is it safe?
Leetcode (763) -- dividing letter ranges
leetcode:1567. Length of the longest subarray whose product is a positive number [dp[i] indicates the maximum length ending with I]
Unity布料系统_Cloth组件(包含动态调用相关)
中金财富开户安全吗?我想开户炒股。
大龄程序员的一些出路
[mixed programming JNI] Part 9: JNI summary
Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
同花顺注册开户有没有什么风险?安全吗?
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
leetcode:141. Circular linked list [hash table + speed pointer]
[mixed programming JNI] Part 12 jnaerator
Yolov6: the fast and accurate target detection framework is open source
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
Is it safe for CICC fortune to open an account? I want to open an account to speculate in stocks.
FPGA -vga display
Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,
AI智能抠图工具--头发丝都可见
How SAP Spartacus default routing configuration works