当前位置:网站首页>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 !
边栏推荐
- 一个实习生的CnosDB之旅
- Learn online case practice
- Graduation season is both a farewell and a new beginning
- What is ThreadLocal memory leak and how to solve it
- EMC circuit protection device for surge and impulse current protection
- Windows installation WSL (II)
- 【QT】Qt 使用MSVC2017找不到编译器的解决办法
- USB-IF协会与各种接口的由来
- 启牛学院开户安全的吗?开户怎么开?
- I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
猜你喜欢

数据库--SqlServer详解

【QT】對於Qt MSVC 2017無法編譯的問題解决

Shell process control

2023 Lexus ES products have been announced, which makes great progress this time

牛客-练习赛101-推理小丑

起床困难综合症(按位贪心)
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

Guide d'installation du serveur SQL

Leetcode skimming: stack and queue 02 (realizing stack with queue)

关联性——组内相关系数
随机推荐
The origin of usb-if Association and various interfaces
13 MySQL constraint
Cmake engineering related
Node -- egg implements the interface of uploading files
4. Object mapping Mapstercover
Difficult to get up syndrome (bit by bit greed)
Example explanation: move graph explorer to jupyterlab
Jielizhi, production line assembly link [chapter]
LeetCode 0241.为运算表达式设计优先级 - DFS
微信小程序缓存过期时间的相关设置(推荐)
使用htaccess文件禁止目录里的脚本执行权限
二叉搜索树的创建,查找,添加,删除操作
Shell custom function
起床困难综合症(按位贪心)
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
[CTF] bjdctf 2020 Bar _ Bacystack2
【QT】QtCreator卸载与安装(非正常状态)
[QT] QT cannot find a solution to the compiler using msvc2017
Windows10 install WSL (I) (wslregisterdistribution error)
【QT】對於Qt MSVC 2017無法編譯的問題解决



