Skip to content

Commit 4854e44

Browse files
committed
new recursion code
1 parent eb94976 commit 4854e44

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

array/calendar.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import List
12
from sortedcontainers import SortedDict
23

34
# https://leetcode.com/problems/my-calendar-i/
@@ -108,3 +109,19 @@ def book(self, start: int, end: int) -> bool:
108109
# similar problems:
109110
# https://leetcode.com/problems/car-pooling/
110111
# https://leetcode.com/problems/corporate-flight-bookings/submissions/
112+
113+
114+
# https://leetcode.com/problems/corporate-flight-bookings/
115+
class Solution:
116+
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
117+
118+
hm = [0] * (n + 1)
119+
120+
for i, j, v in bookings:
121+
hm[i - 1] += v
122+
hm[j] -= v
123+
124+
for i in range(1, n):
125+
hm[i] += hm[i - 1]
126+
127+
return hm[:-1]

binary_search/3sumCloset.py

Whitespace-only changes.

recursion/letter_combinations.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def letterCombinations(digits):
2+
3+
res = []
4+
5+
hm = {
6+
"2": "abc",
7+
"3": "def",
8+
"4": "ghi",
9+
"5": "jkl",
10+
"6": "mno",
11+
"7": "pqrs",
12+
"8": "tuv",
13+
"9": "wxyz",
14+
}
15+
16+
def dfs(i, curr):
17+
18+
if len(curr) == len(digits):
19+
res.append(curr)
20+
return
21+
22+
for c in hm[digits[i]]:
23+
dfs(i + 1, curr + c)
24+
25+
if digits:
26+
dfs(0, "")
27+
return res
28+
29+
30+
print(letterCombinations("23"))
31+
print(letterCombinations("234"))
32+
print(letterCombinations("2345"))
33+
print(letterCombinations("23456"))

recursion/regular_pattern_matching.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# https://leetcode.com/problems/regular-expression-matching/
2+
3+
4+
def isMatch(s, p):
5+
6+
memo = {}
7+
8+
def helper(i, j):
9+
10+
if j >= len(p):
11+
return i >= len(s)
12+
13+
if (i, j) in memo:
14+
return memo[(i, j)]
15+
16+
first_char = i < len(s) and (p[j] == s[i] or p[j] == ".")
17+
18+
if j + 1 < len(p) and p[j + 1] == "*":
19+
20+
notTake = helper(i, j + 2)
21+
take = first_char and helper(i + 1, j)
22+
23+
memo[(i, j)] = notTake or take
24+
25+
return memo[(i, j)]
26+
27+
memo[(i, j)] = first_char and helper(i + 1, j + 1)
28+
29+
return memo[(i, j)]
30+
31+
return helper(0, 0)
32+
33+
34+
print(isMatch("aa", "a")) # False
35+
print(isMatch("aa", "a*")) # True
36+
print(isMatch("ab", ".*")) # True

0 commit comments

Comments
 (0)