Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16a250a

Browse files
add 1 leetcodes
1 parent6c2df1d commit16a250a

File tree

3 files changed

+118
-45
lines changed

3 files changed

+118
-45
lines changed

‎.idea/workspace.xml

Lines changed: 78 additions & 44 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Target Offer/滑动窗口的最大值.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# -*- coding:utf-8 -*-
1010
classSolution:
1111
defmaxInWindows(self,num,size):
12-
ifnum==Noneorlen(num)<=0orsize<=0:
12+
ifnotnumorsize<=0:
1313
return []
1414
deque= []
1515
iflen(num)>=size:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given an array 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.
3+
4+
For example,
5+
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
6+
7+
Window position Max
8+
--------------- -----
9+
[1 3 -1] -3 5 3 6 7 3
10+
1 [3 -1 -3] 5 3 6 7 3
11+
1 3 [-1 -3 5] 3 6 7 5
12+
1 3 -1 [-3 5 3] 6 7 5
13+
1 3 -1 -3 [5 3 6] 7 6
14+
1 3 -1 -3 5 [3 6 7] 7
15+
Therefore, return the max sliding window as [3,3,5,5,6,7].
16+
'''
17+
classSolution(object):
18+
defmaxSlidingWindow(self,nums,k):
19+
ifnotnumsork<=0:
20+
return []
21+
'''
22+
if you want to modify time complexity, you can write below:
23+
from collections import deque
24+
res, queue = [], deque()
25+
and replace queue.pop(0) with queue.popleft()
26+
'''
27+
res,queue= [], []
28+
forind,valinenumerate(nums):
29+
ifqueueandqueue[0]<=ind-k:
30+
queue.pop(0)
31+
whilequeueandnums[queue[-1]]<val:
32+
queue.pop()
33+
queue.append(ind)
34+
ifind+1>=k:
35+
res.append(nums[queue[0]])
36+
returnres
37+
38+
s=Solution()
39+
print(s.maxSlidingWindow([1,3,-1,-3,5,3,6,7],3))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp