190 - 239 滑动窗口最大值

题目

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回滑动窗口中的最大值。

示例:

输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7]

解释:

  滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

提示:

你可以假设 k 总是有效的,在输入数组不为空的情况下,1 ≤ k ≤ 输入数组的大小。

进阶:

你能在线性时间复杂度内解决此题吗?

解答

想到的方法是,用一个变量存当前最大值,然后for循环遍历push进答案里面

Runtime: 156 ms, faster than 93.11% of Python3 online submissions for Sliding Window Maximum.

Memory Usage: 19.5 MB, less than 88.46% of Python3 online submissions for Sliding Window Maximum.

居然真的成功了

刷了190题之后,有生之年,不看题解破了一道困难题

可把我激动坏了

可喜可贺、可喜可贺

哈哈哈哈哈我简直太帅了

而且仔细一看效率还不错吼

哈哈哈哈

go版本!

Runtime: 820 ms, faster than 73.52% of Go online submissions for Sliding Window Maximum.

Memory Usage: 8.6 MB, less than 100.00% of Go online submissions for Sliding Window Maximum.

不知道为啥那么慢

Runtime: 96 ms, faster than 58.20% of JavaScript online submissions for Sliding Window Maximum.

Memory Usage: 41.4 MB, less than 100.00% of JavaScript online submissions for Sliding Window Maximum.

暴力法

https://leetcode-cn.com/problems/sliding-window-maximum/solution/hua-dong-chuang-kou-zui-da-zhi-by-leetcode-3/

这个python版本好简洁。。

Runtime: 704 ms, faster than 20.95% of Python3 online submissions for Sliding Window Maximum.

Memory Usage: 19.3 MB, less than 96.15% of Python3 online submissions for Sliding Window Maximum.

双向队列

https://leetcode-cn.com/problems/sliding-window-maximum/solution/dan-diao-dui-lie-by-labuladong/

不得不说,这次官方题解比lab大佬说得清楚😏

简言之,双向队列。两边都能push和pop。底层用链表就能实现。

插入的时候,把小于自己的元素都删掉。相当于在插入过程中就过滤了一遍小的。

Runtime: 192 ms, faster than 43.50% of Python3 online submissions for Sliding Window Maximum.

Memory Usage: 19.4 MB, less than 88.46% of Python3 online submissions for Sliding Window Maximum.

光头大神解法:

https://leetcode.com/problems/sliding-window-maximum/discuss/65901/9-lines-Ruby-11-lines-Python-O(n)

Runtime: 168 ms, faster than 75.46% of Python3 online submissions for Sliding Window Maximum.

Memory Usage: 19.4 MB, less than 88.46% of Python3 online submissions for Sliding Window Maximum.

动态规划

划分数组,然后用两个数组,存从左向右,和从右向左的最大值。

然后一次遍历,去两头最大值

image.png

Runtime: 192 ms, faster than 37.86% of Python3 online submissions for Sliding Window Maximum.

Memory Usage: 19.5 MB, less than 88.46% of Python3 online submissions for Sliding Window Maximum.

Last updated

Was this helpful?