Skip to content

Commit d481290

Browse files
committed
add two more
1 parent a800dbe commit d481290

4 files changed

+79
-0
lines changed

Diff for: src/minimum_window_substr.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def minWindow(self, s: str, t: str) -> str:
3+
if t == "":
4+
return ""
5+
6+
countT, window = {}, {}
7+
8+
for c in t:
9+
countT[c] = 1 + countT.get(c, 0)
10+
11+
have, need = 0, len(countT)
12+
13+
res, length = [-1, -1], float("infinity")
14+
left = 0
15+
for right in range(len(s)):
16+
c = s[right]
17+
window[c] = 1 + window.get(c, 0)
18+
19+
if c in countT and window[c] == countT[c]:
20+
have += 1
21+
22+
while have == need:
23+
if (right - left + 1) < length:
24+
res = [left, right]
25+
length = right - left + 1
26+
27+
window[s[left]] -= 1
28+
if s[left] in countT and window[s[left]] < countT[s[left]]:
29+
have -= 1
30+
left += 1
31+
32+
left, right = res
33+
return s[left : right + 1] if length != float("infinity") else ""

Diff for: src/sliding_window_maximum.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
from collections import deque
3+
4+
5+
class Solution:
6+
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
7+
output = []
8+
q = deque()
9+
left = right = 0
10+
while right < len(nums):
11+
while q and nums[q[-1]] < nums[right]:
12+
q.pop()
13+
q.append(right)
14+
15+
if left > q[0]:
16+
q.popleft()
17+
if (right + 1) >= k:
18+
output.append(nums[q[0]])
19+
left += 1
20+
right += 1
21+
22+
return output

Diff for: tests/test_minimum_window_substr.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from minimum_window_substr import Solution
2+
3+
testcase = [
4+
{"testcase": ("ADOBECODEBANC", "ABC"), "result": "BANC"}
5+
]
6+
7+
def test_minimum_window_substr():
8+
for tc in testcase:
9+
s, t = tc["testcase"]
10+
ans = Solution().minWindow(s, t)
11+
assert ans == tc["result"]

Diff for: tests/test_sliding_window_maximum.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from sliding_window_maximum import Solution
2+
3+
testcase = [
4+
{"testcase": [1, 3, -1, -3, 5, 3, 6, 7], "k": 3, "result": [3, 3, 5, 5, 6, 7]}
5+
]
6+
7+
8+
def test_sliding_window_maximum():
9+
for tc in testcase:
10+
nums = tc["testcase"]
11+
k = tc["k"]
12+
ans = Solution().maxSlidingWindow(nums, k)
13+
assert ans == tc["result"]

0 commit comments

Comments
 (0)