-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidingWindow.py
More file actions
38 lines (34 loc) · 870 Bytes
/
SlidingWindow.py
File metadata and controls
38 lines (34 loc) · 870 Bytes
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
37
38
def nextLarger(nums):
stack = []
ret = [0] * len(nums)
for i in range(len(nums)):
while stack and nums[i] > nums[stack[-1]]:
ret[stack[-1]] = nums[i]
stack.pop()
stack.append(i)
return ret
print(nextLarger([1, 4, 2, 3, 5]))
def characterReplacement(s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
# maintain the count of letter in current slide
c_dic = {}
for c in s: c_dic[c] = 0
max_count = 0
max_l = 0
if len(s) == 0:
return 0
else:
l = 0
for r in range(len(s)):
c_dic[s[r]] += 1
max_count = max([max_count, c_dic[s[r]]])
if max_coun t + k > = r - l + 1:
max_l = max([max_l, r - l + 1])
else:
c_dic[s[l]] -= 1
l += 1
return max_l