Skip to content

Commit 9097038

Browse files
committed
[Feat] Add more implementation on DP
1 parent 81321ba commit 9097038

7 files changed

+130
-0
lines changed

152-MaximumProductSubarray.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxProduct(self, nums: List[int]) -> int:
3+
res = max(nums)
4+
curMin, curMax = 1, 1
5+
6+
for num in nums:
7+
if num == 0:
8+
curMin, curMax = 1, 1
9+
continue
10+
temp = num * curMax
11+
curMax = max(num * curMax, num * curMin, num)
12+
curMin = min(temp, num * curMin, num)
13+
res = max(curMax, res)
14+
15+
return res

202-HappyNumber.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isHappy(self, n: int) -> bool:
3+
history = set()
4+
check = 0
5+
tempStr = str(n)
6+
7+
while (check != 1) :
8+
temp = 0
9+
for i in tempStr:
10+
temp += (int(i)**2)
11+
check = temp
12+
tempStr = str(check)
13+
14+
if (check in history) :
15+
return False
16+
else:
17+
history.add(check)
18+
19+
return True

213-HouseRobberII.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def help(self, nums: List[int]) -> int:
3+
rob1, rob2 = 0, 0
4+
5+
# rob1, rob2, n, n+1
6+
for n in nums:
7+
temp = max(rob1 + n, rob2)
8+
rob1 = rob2
9+
rob2 = temp
10+
11+
return rob2
12+
13+
def rob(self, nums: List[int]) -> int:
14+
if len(nums) == 1:
15+
return nums[0]
16+
return max(self.help(nums[1:]), self.help(nums[:-1]))

322-CoinChange.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
# Dynamic Programming
4+
# isi data semaksimal mungkin, karna kita bakal pilih minimum
5+
dp = [amount + 1] * (amount + 1)
6+
dp[0] = 0
7+
8+
for i in range(1, amount + 1):
9+
for coin in coins:
10+
if amount - coin >= 0:
11+
dp[i] = min(dp[i], 1 + dp[i - coin])
12+
13+
if dp[amount] == amount + 1:
14+
return -1
15+
else:
16+
return dp[amount]

5-LongestPalindromicSubstring.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> str:
3+
res = ""
4+
resLen = 0
5+
6+
for i in range(len(s)):
7+
# Odd
8+
l, r = i, i
9+
while (l >= 0 and r <= len(s) - 1) and (s[l] == s[r]):
10+
if r - l + 1 > resLen:
11+
res = s[l : r + 1]
12+
resLen = r - l + 1
13+
l -= 1
14+
r += 1
15+
16+
# Even
17+
l, r = i, i + 1
18+
while (l >= 0 and r <= len(s) - 1) and (s[l] == s[r]):
19+
if r - l + 1 > resLen:
20+
res = s[l : r + 1]
21+
resLen = r - l + 1
22+
l -= 1
23+
r += 1
24+
return res

647-PalindromicSubstrings.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
res = 0
4+
5+
for i in range(len(s)):
6+
# Odd
7+
l, r = i, i
8+
while (l >= 0 and r <= len(s) - 1) and (s[l] == s[r]):
9+
res += 1
10+
l -= 1
11+
r += 1
12+
13+
# Even
14+
l, r = i, i + 1
15+
while (l >= 0 and r <= len(s) - 1) and (s[l] == s[r]):
16+
res += 1
17+
l -= 1
18+
r += 1
19+
return res

91-DecodeWays.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
# in case string kosong return 1
4+
history = {len(s): 1}
5+
6+
def dfs(i):
7+
if i in history:
8+
return history[i]
9+
if s[i] == "0":
10+
return 0
11+
12+
res = dfs(i + 1)
13+
if i + 1 < len(s) and (
14+
s[i] == "1" or (s[i] == "2" and s[i + 1] in "0123456")
15+
):
16+
res += dfs(i + 2)
17+
18+
history[i] = res
19+
return res
20+
21+
return dfs(0)

0 commit comments

Comments
 (0)