Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
shinmao
algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm
/
leetcode
/
Leetcode239.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
36 lines (35 loc) · 1.58 KB
main
Breadcrumbs
algorithm
/
leetcode
/
Leetcode239.cpp
Copy path
Top
File metadata and controls
Code
Blame
36 lines (35 loc) · 1.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 發現只要維護一個單調的遞減序列
// 最後剩下的就會是答案
// 我們要討論的是該數字可不可能當max
// 假設窗口移動,有新的拜訪者,我們拿來跟窗口最右邊的A比較
// A比拜訪者還小
// 我們一定知道,只要有這個拜訪者一起待在同一個窗口
// 那麼A一定當不成max
// 好死不死,sliding window A一定會比拜訪者先離開窗口,所以要先當max是不可能的了
// 然後繼這個拜訪者後的拜訪者比他小沒關係,畢竟第一個拜訪者會先離開窗口,第二個拜訪者還是有可能成為window的max的
// 因此deque有三種操作
// 從尾端插入:拜訪者
// 從尾端刪除:尾端的人比拜訪者小
// 從頭部刪除:窗口向後移動時
// 也由於我們需要在頭部和尾端操作,所以用deque
// 總時間:O(n)
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
if(n == 0 || k > nums.size())
return {};
deque<int> dq;
vector<int> res;
for(int i = 0; i < n; i++) {
if(!dq.empty() && dq.front() == i - k) // 窗口移動,頭部刪除
dq.pop_front();
while(!dq.empty() && nums[dq.back()] < nums[i]) // 窗口尾端的人比拜訪者還小
dq.pop_back();
dq.push_back(i); // 新拜訪者
if(i >= k - 1) // 只要我們完成第一個窗口的置入,就可以開始找尋max
res.push_back(nums[dq.front()]);
}
return res;
}
};
You can’t perform that action at this time.