当前位置:网站首页>Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
2022-07-02 00:27:00 【Taotao can't learn English】
239. Maximum sliding window
Given an array nums, There is a size of k The sliding window of the array moves from the leftmost side to the rightmost side of the array . You can only see... In the sliding window k A digital . The sliding window moves only one bit to the right at a time .
Returns the maximum value in the sliding window .
Advanced :
Can you solve this problem in linear time complexity ?
Tips :
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- 1 <= k <= nums.length
All the way through the stumbling .
I got this question originally , It seems not difficult , It's still difficult ???
Write the code in a few lines
public int[] maxSlidingWindow(int[] nums, int k) {
/** * Input :nums = [1,3,-1,-3,5,3,6,7], k = 3 * Output :[3,3,5,5,6,7] * common 8 Number , Slide in groups of three , It can be made up of 8-3+1 Group , namely 6 Group */
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
for (int i = 0; i < nums.length - k + 1; i++) {
int max = -10000;
for (int j = i; j < i + k; j++) {
if (nums[j] > max) {
max = nums[j];
}
}
result[i] = max;
}
return result;
}
I can only say : What a long test case ...
Then improve your thinking , Change violent cracking to sliding . Divide a window into the following parts .
left Is the current maximum expiration
right Is the new value
mid Is the last maximum , Is the maximum of that old interval , As shown in the figure : At present mid=3
Every time the window moves back , Let's see if the previous maximum can still be used ( Whether the maximum value is equal to left The value of is the same , The same cannot be used , You have to traverse the window again to find the maximum value ); If you can use it, continue to use it .
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * If the maximum value of the current range element is still the maximum value of the next range , Continue to use . * Here we need to judge whether the current position has passed */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
// front k The two have been compared
int resIndex = 1;
// Abstract it out , Left boundary value , The maximum value of the middle part , Right boundary value
for (int i = k; i < nums.length; i++) {
// Just expired left boundary
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
if (left>mid-1) {
// If expired , You have to find the maximum again
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
// If the new element is larger than the original
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}
emmmm There's progress , This pass 49 individual , There are only two left . After pondering hard , Finally let me see something different .
So many kinds . In order to improve efficiency , In addition to proving that the maximum value is and left The value of is the same , The update proves that the maximum value is just eliminated left The maximum of . So I changed a line of code . Passed .
public static int[] maxSlidingWindow2(int[] nums, int k) {
/** * Input :nums = [1,3,-1,-3,5,3,6,7], k = 3 * Output :[3,3,5,5,6,7] * common 8 Number , Slide in groups of three , It can be made up of 8-3+1 Group , namely 6 Group */
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * If the maximum value of the current range element is still the maximum value of the next range , Continue to use . * Here we need to judge whether the current position has passed */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
// front k The two have been compared
int resIndex = 1;
// Abstract it out , Left boundary value , The maximum value of the middle part , Right boundary value
for (int i = k; i < nums.length; i++) {
// Just expired left boundary
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
// Partial optimization , Prevent consecutive repeated numbers
// If left That's the maximum
if (left > mid - 1 && left != nums[i - k + 1]) {
// If expired , You have to find the maximum again
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
// If the new element is larger than the original
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}
perfect AC !
边栏推荐
- 【QT】QtCreator卸载与安装(非正常状态)
- 时间复杂度与空间复杂度
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
- Example explanation: move graph explorer to jupyterlab
- [embedded system course design] a single key controls the LED light
- [CTF] bjdctf 2020 Bar _ Bacystack2
- From 20s to 500ms, I used these three methods
- It's nothing to be utilitarian!
- Operate database transactions with jpatractionmanager
- EMC circuit protection device for surge and impulse current protection
猜你喜欢
Correlation - intra group correlation coefficient
SQL数据分析之流程控制语句【if,case...when详解】
Download the online video m3u8 tutorial
Learn online case practice
Jielizhi, production line assembly link [chapter]
起床困难综合症(按位贪心)
Graduation season is both a farewell and a new beginning
EMC circuit protection device for surge and impulse current protection
时间复杂度与空间复杂度
Leetcode skimming: stack and queue 02 (realizing stack with queue)
随机推荐
Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
实例讲解将Graph Explorer搬上JupyterLab
Iota in golang
[cmake] cmake configuration in QT Creator
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
回顾数据脱敏系统
挖财学堂开户打新债安全可靠嘛?
Learn online case practice
leetcode96不同的二叉搜索樹
Use es to realize epidemic map or take out order function (including code and data)
export default 导出的对象,不能解构问题,和module.exports的区别
The difference between timer and scheduledthreadpoolexecutor
Graduation season is both a farewell and a new beginning
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
Heketi record
UDS bootloader of s32kxxx bootloader
Record the accidental success and failure of uploading large files
Download the online video m3u8 tutorial
What is the purpose of ERP project implementation plan?
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速