Skip to content

Commit 0104612

Browse files
committed
Add solutions
1 parent 629c769 commit 0104612

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

coin-change/bhyun-kim.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
322. Coin Change
3+
https://leetcode.com/problems/coin-change/
4+
5+
Solution: Dynamic Programming
6+
To solve this problem, we can use dynamic programming.
7+
We can define a dp array to store the minimum number of coins needed to make up the amount.
8+
We can iterate through the coins and the amount to update the dp array.
9+
The minimum number of coins needed to make up the amount is the minimum of the current dp value and the dp value at the previous amount minus the current coin value plus one.
10+
11+
- Initialize a dp array with length amount + 1 and set dp[0] to 0.
12+
- Iterate through the coins and the amount to update the dp array.
13+
- Return dp[amount] if it is less than or equal to the amount, otherwise return -1.
14+
15+
Time complexity: O(n * m)
16+
- n is the amount.
17+
- m is the number of coins.
18+
- We iterate through the coins and the amount to update the dp array.
19+
20+
Space complexity: O(n)
21+
- We use a dp array of length amount + 1 to store the minimum number of coins needed to make up each amount.
22+
"""
23+
24+
from typing import List
25+
26+
27+
class Solution:
28+
def coinChange(self, coins: List[int], amount: int) -> int:
29+
dp = [amount + 1] * (amount + 1)
30+
dp[0] = 0 # Base case
31+
32+
for coin in coins:
33+
for i in range(coin, amount + 1):
34+
dp[i] = min(dp[i], dp[i - coin] + 1)
35+
36+
return dp[amount] if dp[amount] != amount + 1 else -1

decode-ways/bhyun-kim.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
91. Decode Ways
3+
https://leetcode.com/problems/decode-ways/
4+
5+
Solution: Dynamic Programming
6+
We can use dynamic programming to count the number of ways to decode the input string.
7+
We can define a dp array to store the number of ways to decode the string up to the current index.
8+
We can iterate through the input string and update the dp array based on the current character and the previous characters.
9+
The number of ways to decode the string up to the current index is the sum of the number of ways to decode the previous two characters.
10+
11+
- Initialize a dp array with length n + 1 and set dp[0] to 1.
12+
- Iterate through the input string starting from the first character.
13+
- Update dp[i] based on the current character and the previous characters.
14+
- Return dp[n].
15+
16+
Time complexity: O(n)
17+
- We iterate through the input string once.
18+
19+
Space complexity: O(n)
20+
- We use a dp array of length n + 1 to store the number of ways to decode the string up to each index.
21+
"""
22+
23+
24+
class Solution:
25+
def numDecodings(self, s: str) -> int:
26+
if not s:
27+
return 0
28+
29+
n = len(s)
30+
dp = [0] * (n + 1)
31+
dp[0] = 1
32+
33+
for i in range(1, n + 1):
34+
if s[i - 1] != "0":
35+
dp[i] += dp[i - 1]
36+
if i > 1 and "10" <= s[i - 2 : i] <= "26":
37+
dp[i] += dp[i - 2]
38+
39+
return dp[n]

maximum-product-subarray/bhyun-kim.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
152. Maximum Product Subarray
3+
https://leetcode.com/problems/maximum-product-subarray/
4+
5+
Solution: Dynamic Programming
6+
To solve this problem, we need to keep track of the maximum product and minimum product so far.
7+
The minimum product can become the maximum product when multiplied by a negative number.
8+
9+
- Initialize min_so_far and max_so_far to the first element of the input array.
10+
- Iterate through the input array starting from the second element.
11+
- Update min_so_far and max_so_far with the current element.
12+
- Update max_product with the maximum of max_so_far and max_product.
13+
- Return max_product.
14+
15+
Time complexity: O(n)
16+
- We iterate through the input array once.
17+
18+
Space complexity: O(1)
19+
- We use only a constant amount of space.
20+
21+
22+
"""
23+
24+
from typing import List
25+
26+
27+
class Solution:
28+
def maxProduct(self, nums: List[int]) -> int:
29+
min_so_far = nums[0]
30+
max_so_far = nums[0]
31+
max_product = max_so_far
32+
33+
for i in range(1, len(nums)):
34+
curr = nums[i]
35+
min_so_far, max_so_far = min(
36+
curr, max_so_far * curr, min_so_far * curr
37+
), max(curr, max_so_far * curr, min_so_far * curr)
38+
max_product = max(max_so_far, max_product)
39+
40+
return max_product

word-break/bhyun-kim.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
139. Word Break
3+
https://leetcode.com/problems/word-break/
4+
5+
Solution:
6+
This problem can be solved using a recursive approach with memoization.
7+
We can define a recursive function that takes an index as an argument.
8+
The function checks if the substring from the current index to the end of the string can be broken into words from the dictionary.
9+
We can use memoization to store the results of subproblems to avoid redundant calculations.
10+
11+
- Define a recursive function that takes an index as an argument.
12+
- Check if the substring from the current index to the end of the string can be broken into words from the dictionary.
13+
- Use memoization to store the results of subproblems.
14+
- Return the result of the recursive function.
15+
16+
Time complexity: O(n*m*k)
17+
- n is the length of the input string.
18+
- m is the number of words in the dictionary.
19+
- k is the average length of the words in the dictionary
20+
- The time complexity is O(n*m*k) due to the nested loops.
21+
22+
Space complexity: O(n)
23+
- We use memoization to store the results of subproblems.
24+
- The space complexity is O(n) to store the results of subproblems.
25+
"""
26+
27+
from functools import cache
28+
from typing import List
29+
30+
31+
class Solution:
32+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
33+
@cache
34+
def dp(i):
35+
if i < 0:
36+
return True
37+
38+
for word in wordDict:
39+
if s[i - len(word) + 1 : i + 1] == word and dp(i - len(word)):
40+
return True
41+
42+
return False
43+
44+
return dp(len(s) - 1)

0 commit comments

Comments
 (0)