From 4619998f5b7f4b2f16fc20f9649ac91a568a20d8 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:07 +0800 Subject: [PATCH] =?UTF-8?q?Update=2059.=20=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/59. 滑动窗口的最大值.md | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/notes/59. 滑动窗口的最大值.md b/notes/59. 滑动窗口的最大值.md index ed3f39d7..6958656e 100644 --- a/notes/59. 滑动窗口的最大值.md +++ b/notes/59. 滑动窗口的最大值.md @@ -2,7 +2,7 @@ ## 题目链接 -[牛客网](https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqId=11217&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/) ## 题目描述 @@ -19,18 +19,21 @@ 假设窗口的大小为 M,数组的长度为 N。在窗口向右移动时,需要先在堆中删除离开窗口的元素,并将新到达的元素添加到堆中,这两个操作的时间复杂度都为 log2M,因此算法的时间复杂度为 O(Nlog2M),空间复杂度为 O(M)。 ```java -public ArrayList maxInWindows(int[] num, int size) { - ArrayList ret = new ArrayList<>(); - if (size > num.length || size < 1) - return ret; +public int[] maxAltitude(int[] heights, int limit) { + if (heights == null || heights.length == 0 || limit <= 0) { + return new int[0]; + } + int[] ret = new int[heights.length - limit + 1]; PriorityQueue heap = new PriorityQueue<>((o1, o2) -> o2 - o1); /* 大顶堆 */ - for (int i = 0; i < size; i++) - heap.add(num[i]); - ret.add(heap.peek()); - for (int i = 0, j = i + size; j < num.length; i++, j++) { /* 维护一个大小为 size 的大顶堆 */ - heap.remove(num[i]); - heap.add(num[j]); - ret.add(heap.peek()); + for (int i = 0; i < limit; i++) { + heap.add(heights[i]); + } + int idx = 0; + ret[idx++] = heap.peek(); + for (int i = 0, j = i + limit; j < heights.length; i++, j++) { /* 维护一个大小为 limit 的大顶堆 */ + heap.remove(heights[i]); + heap.add(heights[j]); + ret[idx++] = heap.peek(); } return ret; }