尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
千问    LeetCode 239. Sliding Window Maximum Java Implement
This is a classic monotonic deque problem. The key idea is to maintain a deque that stores indices of elements in decreasing order of their values — so the front of the deque always points to the current window’s maximum.Core IdeaFor each new element nums[i]:Remove expired indices from the front if they’re outside the window (deque.peekFirst() i - k).Remove smaller elements from the back — if nums[i] is larger, those smaller elements can never be the max again.Add current index to the back.Record the max (front of deque) once the first full window is formed (i k - 1).Java Implementationimport java.util.Deque;import java.util.ArrayDeque;class Solution {public int[] maxSlidingWindow(int[] nums, int k) {if (nums null || nums.length 0 || k 0) {return new int[0];}int n nums.length; int[] result new int[n - k 1]; DequeInteger deque new ArrayDeque(); // stores indices for (int i 0; i n; i) { // 1. Remove indices outside the window if (!deque.isEmpty() deque.peekFirst() i - k) { deque.pollFirst(); } // 2. Remove elements smaller than nums[i] from the back while (!deque.isEmpty() nums[deque.peekLast()] nums[i]) { deque.pollLast(); } // 3. Add current index deque.offerLast(i); // 4. Record max once the first window is complete if (i k - 1) { result[i - k 1] nums[deque.peekFirst()]; } } return result; }}Walkthrough with nums [1,3,-1,-3,5,3,6,7], k 3i nums[i] deque (indices) deque (values) result0 1 [0] [1] —1 3 [1] [3] —2 -1 [1,2] [3,-1] 33 -3 [1,2,3] [3,-1,-3] 34 5 [4] [5] 55 3 [4,5] [5,3] 56 6 [6] [6] 67 7 [7] [7] 7Output: [3, 3, 5, 5, 6, 7] ✓ComplexityTime: O(n) — each element is added and removed at most once from the deque.Space: O(k) — the deque holds at most k indices.Key Interview PointsWhy store indices instead of values? Storing indices lets us easily check whether the front element is still within the current window (index i - k).Why when removing from back? Using (not just ) ensures that when equal elements exist, we keep the newer one, which stays in the window longer.Why not a max-heap? A heap gives O(n log k), and you’d also need lazy deletion to handle expired elements. The deque approach is strictly better at O(n).Common bug: Forgetting to remove expired indices before reading the max. Always clean the front first.Want me to also show the sparse table / RMQ approach? It’s O(n log n) preprocessing O(1) per query, useful if the interviewer asks about arbitrary range maximum queries (not just sliding windows).
RELATED

相关推荐

千问    LeetCode 224. Basic Calculator Java Implement

千问 LeetCode 224. Basic Calculator Java Implement

This is a classic stack sign tracking problem. The key insight is that parentheses only change the sign context — we don’t need to evaluate sub-expressions separately, just propagate the current sign into nested scopes. Core Idea Maintain three things w…

📅 2026/10/11 1:55:07
Outlook / Hotmail 邮箱监控配置流程

Outlook / Hotmail 邮箱监控配置流程

Outlook / Hotmail 邮箱监控配置流程 1. 注册 Microsoft Azure 账号 正常注册通常需要绑定 Visa 信用卡。 没有信用卡时,可尝试走学生通道。 学生通道需要 edu 邮箱。 可通过闲鱼获取 edu 邮箱;请注意合规与账号安全风险。 2. 进入 Microsoft Entra ID 注册完成后,在 Micro…

📅 2026/10/11 1:55:07
AI安全监管,为何越管越松?

AI安全监管,为何越管越松?

最近华盛顿挺热闹的。两份跟AI监管有关的信,几乎同时落地。 一封质疑白宫——有参议员给财长和商务部长写信,问是不是在科技巨头影响下,把强制AI安全筛查改成了自愿的。 另一封冲着一家AI公司——有参议员致信Anthropic CEO,问他一…

📅 2026/10/11 1:55:07
MORE NEWS

更多资讯

📰

STM32寄存器白话手册:手把手寄存器操作点亮LED

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

6款网络工程师效率神器:从抓包到自动化监控的实战指南

干网络这一行,最累人的往往不是技术难题,而是那些重复、琐碎、还不能出错的操作。白天要配网、调策略、查日志,晚上还要蹲告警,别人看我捧着电脑好像很忙,其实大部分时间都花在App之间来回切换、手动重复同样的命令、等…

📰

Trae国际版实战:从配置到Builder模式,AI IDE高效开发指南

Trae国际版这阵子热度挺高,作为一个每天跟代码打交道的开发者,我第一时间装来折腾了一周,把几个主力项目都深度用了一遍。这篇文章不聊官方文档里已经写了的东西,就说说我实际使用中跑通的一套最佳实践:从安装配置、AI…

📰

MyEclipse 10.7汉化完整指南:Babel语言包安装与避坑实践

简介:一份针对 MyEclipse 10.7 的完整汉化资源包,面向中文环境下使用该 Eclipse 系 Java IDE 的开发者,覆盖菜单栏、代码编辑器、调试、运行配置及内置插件界面,可有效消除英文操作门槛,适合日常开发、教学演示与项目迁…

📰

AI芯片软硬件协同实战:算子融合、DMA调度与硅前验证全解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

impeccable:可验证的工程质量标准与四层落地实践

1. “impeccable”不是一句空泛夸奖,而是可拆解、可验证、可复现的专业标准最近在多个技术评审会和设计交付现场,反复听到这个词被高频使用:“这个接口文档写得真impeccable”“UI动效的时序控制达到了impeccable级别”“CI流水线的失败归因逻…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬