Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
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 }}
yi-yun
MyPythonScripts
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
MyPythonScripts
/
Max_windows.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
36 lines (31 loc) · 1.3 KB
master
Breadcrumbs
MyPythonScripts
/
Max_windows.py
Copy path
Top
File metadata and controls
Code
Blame
36 lines (31 loc) · 1.3 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
def maxInWindows(num, size):
# 如果数组 num 不存在,则返回 []
if not num:
return []
# 如果滑动窗口的大小大于数组的大小,或者 size 小于 0,则返回 []
if size > len(num) or size < 1:
return []
# 如果滑动窗口的大小为 1 ,则直接返回原始数组
if size == 1:
return num
# 存放最大值,次大值的数组,和存放输出结果数组的初始化
temp = [0]
res = []
# 对于数组中每一个元素进行判断
for i in range(len(num)):
# 判断第 i 个元素是否可以加入 temp 中
# 如果比当前最大的元素还要大,清空 temp 并把该元素放入数组
# 首先判断当前最大的元素是否过期
if i - temp[0] > size-1:
temp.pop(0)
# 将第 i 个元素与 temp 中的值比较,将小于 i 的值都弹出
while (len(temp) > 0 and num[i] >= num[temp[-1]]):
temp.pop()
# 如果现在 temp 的长度还没有达到最大规模,将元素 i 压入
if len(temp) < size-1:
temp.append(i)
# 只有经过一个完整的窗口才保存当前的最大值
if i >= size-1:
res.append(num[temp[0]])
return res
print(maxInWindows([2, 3, 4, 2, 6, 2, 5, 1], 3))
You can’t perform that action at this time.