Skip to content

Commit 82ca4d9

Browse files
author
wuduhren
committed
updates
1 parent 79b9b8f commit 82ca4d9

11 files changed

+219
-0
lines changed

problems/python3/coin-change.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
dp = [float('inf')]*(amount+1)
4+
dp[0] = 0
5+
for coin in coins:
6+
if coin<len(dp): dp[coin] = 1
7+
8+
coins.sort()
9+
10+
for i in range(1, amount+1):
11+
for coin in coins:
12+
if i-coin<0: break
13+
dp[i] = min(dp[i], dp[i-coin]+1)
14+
return dp[amount] if dp[amount]!=float('inf') else -1

problems/python3/decode-ways.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
dp[i] := up until s[:i] how many possibility?
3+
"""
4+
class Solution:
5+
def numDecodings(self, s: str) -> int:
6+
mapping = set([str(n) for n in range(1, 27)])
7+
N = len(s)
8+
dp = [0]*(N+1)
9+
dp[0] = 1
10+
11+
for i in range(1, N+1):
12+
if i-1>=0 and s[i-1] in mapping: dp[i] += dp[i-1]
13+
if i-2>=0 and s[i-2:i] in mapping: dp[i] += dp[i-2]
14+
return dp[-1]

problems/python3/house-robber-ii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
if len(nums)<=1: return max(nums)
4+
5+
N = len(nums)
6+
dp = [[0, 0] for _ in range(N)]
7+
dp[0][0] = nums[0]
8+
9+
for i in range(1, N):
10+
dp[i][0] = nums[i]+dp[i-1][1]
11+
dp[i][1] = max(dp[i-1])
12+
13+
dp2 = [[0, 0] for _ in range(N)]
14+
for i in range(1, N):
15+
dp2[i][0] = nums[i]+dp2[i-1][1]
16+
dp2[i][1] = max(dp2[i-1])
17+
18+
return max(dp[-1][1], dp2[-1][0])

problems/python3/house-robber.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N), can reduece to O(1).
4+
5+
dp[i][0] := max revenue if house i robbed
6+
dp[i][1] := max revenue if house i not robbed
7+
"""
8+
class Solution:
9+
def rob(self, nums: List[int]) -> int:
10+
N = len(nums)
11+
dp = [[0, 0] for _ in range(N)]
12+
dp[0][0] = nums[0]
13+
14+
for i in range(1, N):
15+
dp[i][0] = nums[i]+dp[i-1][1]
16+
dp[i][1] = max(dp[i-1])
17+
return max(dp[-1])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
3+
#dp[i][j] := number of longest Common Subsequence with text2[:i] and text2[:j]
4+
5+
N = len(text1)
6+
M = len(text2)
7+
8+
dp = [[0]*(M+1) for _ in range(N+1)]
9+
10+
for i in range(1, N+1):
11+
for j in range(1, M+1):
12+
if text1[i-1]==text2[j-1]:
13+
dp[i][j] = dp[i-1][j-1]+1
14+
else:
15+
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
16+
return dp[-1][-1]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Time: O(N^2)
3+
Space: O(N^2)
4+
5+
DP, TLE
6+
"""
7+
class Solution:
8+
def longestPalindrome(self, s: str) -> str:
9+
ans = s[0]
10+
N = len(s)
11+
dp = [[False]*N for _ in range(N)]
12+
13+
for i in range(N): dp[i][i] = True
14+
15+
for l in range(2, N+1):
16+
for i in range(N):
17+
j = i+l-1
18+
if j>=N: continue
19+
dp[i][j] = s[i]==s[j] and (dp[i+1][j-1] or j-1<i+1)
20+
if dp[i][j]: ans = s[i:j+1]
21+
return ans
22+
23+
24+
"""
25+
Time: O(N^2)
26+
Space: O(1)
27+
"""
28+
class Solution:
29+
def longestPalindrome(self, s: str) -> str:
30+
N = len(s)
31+
ans = s[0]
32+
33+
for i in range(N):
34+
l, r = i, i
35+
while l>=0 and r<N and s[l]==s[r]:
36+
if r-l+1>len(ans): ans = s[l:r+1]
37+
l -= 1
38+
r += 1
39+
40+
l, r = i, i+1
41+
while l>=0 and r<N and s[l]==s[r]:
42+
if r-l+1>len(ans): ans = s[l:r+1]
43+
l -= 1
44+
r += 1
45+
return ans
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Time: O(N)
3+
Space: O(N), can be reduce to O(1)
4+
5+
dp[i][0] := The max product from subarray that end with nums[i]
6+
dp[i][1] := The min product from subarray that end with nums[i]
7+
"""
8+
class Solution:
9+
def maxProduct(self, nums: List[int]) -> int:
10+
N = len(nums)
11+
dp = [[1, 1] for _ in range(N+1)]
12+
ans = float('-inf')
13+
14+
for i in range(1, N+1):
15+
dp[i][0] = dp[i][1] = nums[i-1]
16+
17+
if nums[i-1]>0:
18+
dp[i][0] = max(dp[i][0], nums[i-1]*dp[i-1][0])
19+
dp[i][1] = min(dp[i][1], nums[i-1]*dp[i-1][1])
20+
else:
21+
dp[i][0] = max(dp[i][0], nums[i-1]*dp[i-1][1])
22+
dp[i][1] = min(dp[i][1], nums[i-1]*dp[i-1][0])
23+
ans = max(ans, dp[i][0])
24+
25+
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
def countPalindrome(l, r) -> int:
4+
count = 0
5+
while l>=0 and r<N and s[l]==s[r]:
6+
count += 1
7+
l -= 1
8+
r += 1
9+
return count
10+
11+
N = len(s)
12+
ans = 0
13+
for i in range(N):
14+
ans += countPalindrome(i, i)
15+
ans += countPalindrome(i, i+1)
16+
return ans
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Time: O(2^N), 2^0 + 2^1 + 2^2 + 2^3 + ... 2^(N-1) == 2^N-1
3+
Space: O(T), T is the sum of nums
4+
"""
5+
class Solution:
6+
def canPartition(self, nums: List[int]) -> bool:
7+
total = sum(nums)
8+
if total%2!=0: return False
9+
10+
target = total/2
11+
possibleSum = set()
12+
possibleSum.add(0)
13+
for num in nums:
14+
temp = set()
15+
for p in possibleSum:
16+
if p==target or p+num==target: return True
17+
temp.add(p)
18+
temp.add(p+num)
19+
possibleSum = temp
20+
return False

problems/python3/unique-paths.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def uniquePaths(self, m: int, n: int) -> int:
3+
m = m-1 #number of steps need to move down
4+
n = n-1 #number of steps need to move right
5+
6+
#the total combination of m and n to sort will be (m+n)!
7+
#since all "move down" are consider the same, we need to remove the repeatition of it sorting: m!.
8+
#since all "move right" are consider the same, we need to remove the repeatition of it sorting: n!.
9+
#(m+n)!/m!n!
10+
11+
return math.factorial(m+n)//(math.factorial(m)*math.factorial(n))

problems/python3/word-break.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Time: O(N^2 * M). N is the length of the s. M is the number of word in wordDict.
3+
Note that s[i:i+len(word)]==word takes O(N) time.
4+
5+
Space: O(N) for the recursion memory stack size.
6+
7+
dfs(i) := will return starting at index i, if i to the end the string can be separated.
8+
"""
9+
class Solution:
10+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
11+
def dfs(i):
12+
if i==len(s): return True
13+
if i in history and not history[i]: return False
14+
15+
for word in wordDict:
16+
if i+len(word)<=len(s) and s[i:i+len(word)]==word:
17+
history[i] = True
18+
if dfs(i+len(word)): return True
19+
history[i] = False
20+
return False
21+
22+
history = {}
23+
return dfs(0)

0 commit comments

Comments
 (0)