MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Array/SlidingWindowMaximum.py:46–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44import bisect
45
46class Solution(object):
47 def find_bi(self, nums, target):
48 lo = 0
49 hi = len(nums)
50
51 while lo < hi:
52 mid = (lo + hi) // 2
53
54 if nums[mid] == target:
55 return mid
56
57 if nums[mid] < target:
58 lo = mid + 1
59 else:
60 hi = mid
61
62 def maxSlidingWindow(self, nums, k):
63 """
64 :type nums: List[int]
65 :type k: int
66 :rtype: List[int]
67 """
68 if not nums:
69 return []
70
71 x = nums[:k]
72 y = sorted(x)
73 x = deque(x)
74
75 maxes = max(x)
76 result = [maxes]
77
78 for i in nums[k:]:
79 pop = x.popleft()
80 x.append(i)
81
82 index = self.find_bi(y, pop)
83 y.pop(index)
84
85 bisect.insort_left(y, i)
86
87 result.append(y[-1])
88 return result
89

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected