当前位置:网站首页>leetcode刷题:栈与队列07(滑动窗口最大值)
leetcode刷题:栈与队列07(滑动窗口最大值)
2022-07-01 19:16:00 【涛涛英语学不进去】
239. 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回滑动窗口中的最大值。
进阶:
你能在线性时间复杂度内解决此题吗?

提示:
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- 1 <= k <= nums.length
一路做完磕磕绊绊的。
本来拿到这题,好像不难啊,就这还困难???
咔咔几行写完代码
public int[] maxSlidingWindow(int[] nums, int k) {
/** * 输入:nums = [1,3,-1,-3,5,3,6,7], k = 3 * 输出:[3,3,5,5,6,7] * 共 8 个数,三个一组滑动,可组成 8-3+1 组,即 6 组 */
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;
}

对此我只能说:好长的测试用例啊。。。
然后改进思路,把暴力破解改成滑动。把一个窗口分成以下几个部分。
left 是当前过期的最大值
right 是新进来的值
mid 是上一个最大值,是那个旧区间的最大值,如图所示:当前mid=3
每次窗口后移,先看看之前的最大值还能用吧(最大值是否和 left 的值相同,相同则不能用了,就得重新遍历窗口找最大值);如果能用就继续用。
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * 如果当前范围元素的最大值仍然是下一个范围的最大值,则继续用。 * 此处要判断当前位置是否过了 */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
//前k个已经比较过了
int resIndex = 1;
//抽象出来,左边界值,中间部分最大值,右边界值
for (int i = k; i < nums.length; i++) {
//刚过期的左边界
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
if (left>mid-1) {
//如果过期,就得重新再找最大值
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
//如果新元素比原来的大
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}

emmmm有长进,这次通过49个,只剩最后两个了。苦思冥想后,终于让我看到不一样的了。
这么多一样的。为了提高效率,除了要证明最大值是和 left 的值相同,更新证明这个最大值就是刚刚被淘汰的 left 的最大值。于是改了一行代码。通过了。
public static int[] maxSlidingWindow2(int[] nums, int k) {
/** * 输入:nums = [1,3,-1,-3,5,3,6,7], k = 3 * 输出:[3,3,5,5,6,7] * 共 8 个数,三个一组滑动,可组成 8-3+1 组,即 6 组 */
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * 如果当前范围元素的最大值仍然是下一个范围的最大值,则继续用。 * 此处要判断当前位置是否过了 */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
//前k个已经比较过了
int resIndex = 1;
//抽象出来,左边界值,中间部分最大值,右边界值
for (int i = k; i < nums.length; i++) {
//刚过期的左边界
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
//部分优化,防止连续重复数字出现
//如果left就是那个最大值
if (left > mid - 1 && left != nums[i - k + 1]) {
//如果过期,就得重新再找最大值
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
//如果新元素比原来的大
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}

完美 AC !
边栏推荐
- fastDFS入门
- Interesting! Database is also serverless!
- 想得到股票开户的优惠链接,如何得知?在线开户是安全么?
- Problems encountered in installing MySQL in docker Ubuntu container
- PHP gets the external chain address of wechat applet and applet store
- math_利用微分算近似值
- uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理
-
- 柒微自动发卡系统源码
- Common components of flask
猜你喜欢

大厂做狼,小厂做狗?

Win11 how to hide the taskbar? Win11 method to hide the taskbar

Data analysts sound tall? Understand these points before you decide whether to transform

Practical project notes (I) -- creation of virtual machine

RichView RichEdit SRichViewEdit PageSize 页面设置与同步

小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?

宅男壁纸大全微信小程序源码-带动态壁纸支持多种流量主

Customize the insertion of page labels and realize the initial search of similar address books

8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide

STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
随机推荐
随机头像大全,多分类带历史记录微信小程序源码_支持流量主
8K HDR!|为 Chromium 实现 HEVC 硬解 - 原理/实测指南
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
由浅入深学会白盒测试用例设计
How to create a pyramid with openmesh
利用QEventLoop实现同步等待槽函数返回
2022安全员-B证考试练习题模拟考试平台操作
Getting started with fastdfs
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file
《軟件工程導論(第六版)》 張海藩 複習筆記
deb文件安装
2022年低压电工考试试题及答案
Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
EDA工具对芯片产业的重要性知识科普
深度学习 神经网络基础
Entering Ruxin Town, digital intelligence transformation connects "future community"
2022/5/23-2022/5/30
新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码



