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

Appearance settings

Commit 16a250a

Browse filesBrowse files
add 1 leetcodes
1 parent 6c2df1d commit 16a250a
Copy full SHA for 16a250a

File tree

Expand file treeCollapse file tree

3 files changed

+118
-45
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+118
-45
lines changed
Open diff view settings
Collapse file

‎.idea/workspace.xml‎

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+78-44Lines changed: 78 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

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

Copy file name to clipboardExpand all lines: Target Offer/滑动窗口的最大值.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# -*- coding:utf-8 -*-
1010
class Solution:
1111
def maxInWindows(self, num, size):
12-
if num == None or len(num) <= 0 or size <= 0:
12+
if not num or size <= 0:
1313
return []
1414
deque = []
1515
if len(num) >= size:
Collapse file
+39Lines 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+
class Solution(object):
18+
def maxSlidingWindow(self, nums, k):
19+
if not nums or k <= 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+
for ind, val in enumerate(nums):
29+
if queue and queue[0] <= ind - k:
30+
queue.pop(0)
31+
while queue and nums[queue[-1]] < val:
32+
queue.pop()
33+
queue.append(ind)
34+
if ind + 1 >= k:
35+
res.append(nums[queue[0]])
36+
return res
37+
38+
s = Solution()
39+
print(s.maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.