Skip to content

Latest commit

 

History

History
220 lines (178 loc) · 4.76 KB

File metadata and controls

220 lines (178 loc) · 4.76 KB

English Version

题目描述

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

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

 

示例 1:

输入: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

示例 2:

输入:nums = [1], k = 1
输出:[1]

示例 3:

输入:nums = [1,-1], k = 1
输出:[1,-1]

示例 4:

输入:nums = [9,11], k = 2
输出:[11]

示例 5:

输入:nums = [4,-2], k = 2
输出:[4]

 

提示:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • 1 <= k <= nums.length

解法

单调队列。

单调队列常见模型:找出滑动窗口中的最大值/最小值。模板:

q = deque()
for i in range(n):
    # 判断队头是否滑出窗口
    while q and checkout_out(q[0]):
        q.popleft()
    while q and check(q[-1]):
        q.pop()
    q.append(i)

Python3

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        q, res = collections.deque(), []
        for i, num in enumerate(nums):
            if q and i - k + 1 > q[0]:
                q.popleft()
            while q and nums[q[-1]] <= num:
                q.pop()
            q.append(i)
            if i >= k - 1:
                res.append(nums[q[0]])
        return res

Java

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        if (n == 0) {
            return new int[0];
        }
        int[] res = new int[n - k + 1];
        Deque<Integer> q = new LinkedList<>();
        for (int i = 0, j = 0; i < n; ++i) {
            if (!q.isEmpty() && i - k + 1 > q.peekFirst()) {
                q.pollFirst();
            }
            while (!q.isEmpty() && nums[q.peekLast()] <= nums[i]) {
                q.pollLast();
            }
            q.offerLast(i);
            if (i >= k - 1) {
                res[j++] = nums[q.peekFirst()];
            }
        }
        return res;
    }
}

JavaScript

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function (nums, k) {
    let len = nums.length;
    if (len < k) return [];
    let res = [], win = [];
    for (let i = 0; i < k; i++) {
        while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
            win.pop();
        win.push(i);
    }
    res.push(nums[win[0]]);
    for (let i = k; i < len; i++) {
        while (win.length > 0 && nums[i] >= nums[win[win.length - 1]])
            win.pop();
        if (win.length > 0 && win[0] < i - k + 1)
            win.shift();
        win.push(i);
        res.push(nums[win[0]]);
    }
    return res;
};

C++

class Solution {
public:
	vector<int> maxSlidingWindow(vector<int> &nums, int k) {
		vector<int> res;
		deque<int> q;
		for (int i = 0; i < nums.size(); ++i)
		{
			if (!q.empty() && i - k + 1 > q.front())
				q.pop_front();
			while (!q.empty() && nums[q.back()] <= nums[i])
				q.pop_back();
			q.push_back(i);
			if (i >= k - 1)
				res.push_back(nums[q.front()]);
		}
		return res;
	}
};

Go

func maxSlidingWindow(nums []int, k int) []int {
	var res []int
	var q []int
	for i, num := range nums {
		if len(q) > 0 && i-k+1 > q[0] {
			q = q[1:]
		}
		for len(q) > 0 && nums[q[len(q)-1]] <= num {
			q = q[:len(q)-1]
		}
		q = append(q, i)
		if i >= k-1 {
			res = append(res, nums[q[0]])
		}
	}
	return res
}

...