【陪伴式刷题】Day 11|栈和队列| 239.滑动窗口最大值(Sliding Window Maximum)

刷题顺序按照代码随想录建议

题目描述

英文版描述

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max sliding window.

Example 1:

Example 2:

Input: nums = 1, k = 1 Output: 1

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length

英文版地址

leetcode.com/problems/sl...

中文版描述

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

返回 滑动窗口中的最大值

示例 1:

示例 2:

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

提示:

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length

中文版地址

leetcode.cn/problems/sl...

解题方法

俺这版

比较直接的思路,呜呜呜呜~超时了

java 复制代码
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null) {
            return null;
        }
        Queue<Integer> result = new LinkedList<>();
        int len = nums.length;
        
        for (int i = 0; i < len - k + 1; i++) {
            int subMax = nums[i];
            for (int j = i; j < i + k; j++) {
                subMax = Math.max(subMax, nums[j]);
            }
            result.add(subMax);
        }

        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}

复杂度分析

  • 时间复杂度:O(nk),其中 n 是数组 nums 的长度,k是窗口长度,嵌套了两层for循环。
  • 空间复杂度:O(1),只有subMax一个额外变量

官方版

方法一:双端队列

java 复制代码
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums == null) {
            return null;
        }
        ArrayDeque<Integer> queueIndex = new ArrayDeque<>();
        Queue<Integer> result = new LinkedList<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            while (!queueIndex.isEmpty() && queueIndex.peekFirst() < i - k + 1) {
                queueIndex.pollFirst();
            }
            while (!queueIndex.isEmpty() && nums[i] > nums[queueIndex.peekLast()]) {
                queueIndex.pollLast();
            }
            queueIndex.add(i);
            if (i >= k - 1) {
                result.add(nums[queueIndex.peekFirst()]);
            }
        }
        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}
复杂度分析
  • 时间复杂度:O(n),其中 n 是数组 nums 的长度。每一个下标恰好被放入队列一次,并且最多被弹出队列一次,因此时间复杂度为 O(n)。

  • 空间复杂度:O(k),对列里不会存储超过k个元素。

方法二:优先队列

java 复制代码
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] pair1, int[] pair2) {
                return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];
            }
        });
        for (int i = 0; i < k; ++i) {
            pq.offer(new int[]{nums[i], i});
        }
        int[] ans = new int[n - k + 1];
        ans[0] = pq.peek()[0];
        for (int i = k; i < n; ++i) {
            pq.offer(new int[]{nums[i], i});
            while (pq.peek()[1] <= i - k) {
                pq.poll();
            }
            ans[i - k + 1] = pq.peek()[0];
        }
        return ans;
    }
}
复杂度分析
  • 时间复杂度:O(nlog⁡n),其中 n 是数组 nums 的长度。在最坏情况下,数组 nums 中的元素单调递增,那么最终优先队列中包含了所有元素,没有元素被移除。由于将一个元素放入优先队列的时间复杂度为 O(log⁡n),因此总时间复杂度为 O(nlogn)
  • 空间复杂度:O(n),即为优先队列需要使用的空间。这里所有的空间复杂度分析都不考虑返回的答案需要的 O(n) 空间,只计算额外的空间使用
相关推荐
zhanghaha13146 分钟前
HTML系列教程:14_HTML 表单与输入框 <form>、<input> 零基础详解
java·前端·javascript
yxlalm5 小时前
Spring AI+RAG 01-项目背景与技术选型
java·人工智能·spring
程序员清风6 小时前
Java 后端高并发设计:线程池、限流、熔断与降级
java·数据库·oracle
Seoyoneh7 小时前
Agentic Workflow编排架构:云客服从“被动响应”迈向“主动执行”的技术实现
java·开发语言·架构
海南java第二人7 小时前
对外 API 如何保证幂等性?插入数据 / 上传表格场景全方案汇总
java·幂等性
tryxr7 小时前
Chat2Excel 文件服务模块上传文件功能开发
java·项目·oss·easyexcel·文件服务
Tangyuewei7 小时前
Java 写 Agent:模型只是组件
java·开发语言
彧azz8 小时前
Java学习语法篇:变量
java·学习
信誓旦旦的程序猿9 小时前
【量化系统从零构建 #04】存储设计:选型·建库·交易日历
java·人工智能·python·股票数据api·股票数据·股票数据api接口·股票api数据接口
yurenpai(27届找实习中)9 小时前
Java 10 的 var 为什么不能随便用?从订单汇总看懂类型推断与泛型陷阱
java·开发语言·java18