Skip to content

Commit a800dbe

Browse files
committed
add a few questions
1 parent e49763b commit a800dbe

10 files changed

+153
-0
lines changed

src/besttime_sell_buy_stocks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
left, right = 0, 1
7+
maxP = 0
8+
9+
while right < len(prices):
10+
if prices[left] < prices[right]:
11+
maxP = max(maxP, (prices[right] - prices[left]))
12+
else:
13+
left = right
14+
right += 1
15+
16+
return maxP
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def characterReplacement(self, s: str, k: int) -> int:
3+
count = {}
4+
left, maxf, res = 0, 0, 0
5+
6+
for right in range(len(s)):
7+
count[s[right]] = 1 + count.get(s[right], 0)
8+
maxf = max(maxf, count[s[right]])
9+
10+
while (right - left + 1) - maxf > k:
11+
count[s[left]] -= 1
12+
left += 1
13+
14+
res = max(res, right - left + 1)
15+
return res
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
left = 0
4+
charSet = set()
5+
res = 0
6+
7+
for right in range(len(s)):
8+
while s[right] in charSet:
9+
charSet.remove(s[left])
10+
left += 1
11+
charSet.add(s[right])
12+
res = max(res, right - left + 1)
13+
return res

src/permutation_in_string.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def checkInclusion(self, s1: str, s2: str) -> bool:
3+
if len(s1) > len(s2):
4+
return False
5+
6+
s1Count, s2Count = [0] * 26, [0] * 26
7+
for i in range(len(s1)):
8+
s1Count[ord(s1[i]) - ord("a")] += 1
9+
s2Count[ord(s2[i]) - ord("a")] += 1
10+
11+
matches = 0
12+
for i in range(26):
13+
matches += 1 if s1Count[i] == s2Count[i] else 0
14+
15+
left = 0
16+
for right in range(len(s1), len(s2)):
17+
if matches == 26:
18+
return True
19+
20+
index = ord(s2[right]) - ord("a")
21+
s2Count[index] += 1
22+
if s1Count[index] == s2Count[index]:
23+
matches += 1
24+
elif s1Count[index] + 1 == s2Count[index]:
25+
matches -= 1
26+
27+
index = ord(s2[left]) - ord("a")
28+
s2Count[index] -= 1
29+
if s1Count[index] == s2Count[index]:
30+
matches += 1
31+
elif s1Count[index] - 1 == s2Count[index]:
32+
matches -= 1
33+
left += 1
34+
return matches == 26

src/trap_rain_water.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def trap(self, height: List[int]) -> int:
6+
if not height:
7+
return 0
8+
9+
left, right = 0, len(height) - 1
10+
leftMax, rightMax = height[left], height[right]
11+
res = 0
12+
13+
while left < right:
14+
if leftMax < rightMax:
15+
left += 1
16+
leftMax = max(leftMax, height[left])
17+
res += leftMax - height[left]
18+
else:
19+
right -= 1
20+
rightMax = max(rightMax, height[right])
21+
res += rightMax - height[right]
22+
return res
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from besttime_sell_buy_stocks import Solution
2+
3+
4+
testcase = [{"testcase": [7, 1, 5, 3, 6, 4], "result": 5}]
5+
6+
7+
def test_besttime_sell_buy_stock():
8+
for tc in testcase:
9+
prices = tc["testcase"]
10+
ans = Solution().maxProfit(prices)
11+
assert ans == tc["result"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from longest_substring_without_repeating import Solution
2+
3+
testcase = [{"testcase": "abcabcbb", "result": 3}]
4+
5+
6+
def test_longest_substring_without_repeating():
7+
for tc in testcase:
8+
s = tc["testcase"]
9+
ans = Solution().lengthOfLongestSubstring(s)
10+
assert ans == tc["result"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from longest_repeating_char_replacement import Solution
2+
3+
testcase = [{"testcase": "AABABBA", "k": 1, "result": 4}]
4+
5+
6+
def test_longest_repeating_char_replacement():
7+
for tc in testcase:
8+
s = tc["testcase"]
9+
k = tc["k"]
10+
ans = Solution().characterReplacement(s, k)
11+
assert ans == tc["result"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from permutation_in_string import Solution
2+
3+
testcase = [{"testcase": ("ab", "eidbaooo"), "result": True}]
4+
5+
6+
def test_permutation_in_string():
7+
for tc in testcase:
8+
s1 = tc["testcase"][0]
9+
s2 = tc["testcase"][1]
10+
ans = Solution().checkInclusion(s1, s2)
11+
assert ans == tc["result"]

tests/test_trap_rain_water.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from trap_rain_water import Solution
2+
3+
testcase = [{"testcase": [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1], "result": 6}]
4+
5+
6+
def test_trap_rain_water():
7+
for tc in testcase:
8+
height = tc["testcase"]
9+
ans = Solution().trap(height)
10+
assert ans == tc["result"]

0 commit comments

Comments
 (0)