Skip to content

Commit 658e0ac

Browse files
authored
Merge pull request DaleStudy#175 from bhyun-kim/main
[bhyun-kim, 김병현] Week 11 Solutions
2 parents 74e04c9 + 0104612 commit 658e0ac

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-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

palindromic-substrings/bhyun-kim.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
647. Palindromic Substrings
3+
https://leetcode.com/problems/palindromic-substrings/
4+
5+
Solution:
6+
To solve this problem, we can use the expand from center technique.
7+
We can iterate through each character in the string and expand from the center to find palindromic substrings.
8+
We can handle both odd and even length palindromes by expanding from the center and center + 1.
9+
We keep track of the number of palindromic substrings found.
10+
The total number of palindromic substrings is the sum of palindromic substrings found at each character.
11+
12+
Time complexity: O(n^2)
13+
- We iterate through each character in the string.
14+
- For each character, we expand from the center to find palindromic substrings.
15+
- The time complexity is O(n^2) due to the nested loops.
16+
17+
Space complexity: O(1)
18+
- We use constant space to store variables.
19+
- The space complexity is O(1).
20+
"""
21+
22+
23+
class Solution:
24+
def countSubstrings(self, s: str) -> int:
25+
num_pal_strs = 0
26+
27+
def expand_from_center(left, right):
28+
num_pal = 0
29+
while left >= 0 and right < len(s) and s[left] == s[right]:
30+
left -= 1
31+
right += 1
32+
num_pal += 1
33+
return num_pal
34+
35+
for i in range(len(s)):
36+
num_pal_strs += expand_from_center(i, i)
37+
num_pal_strs += expand_from_center(i, i + 1)
38+
39+
return num_pal_strs

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)