Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions coin-change/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
322. Coin Change
https://leetcode.com/problems/coin-change/

Solution: Dynamic Programming
To solve this problem, we can use dynamic programming.
We can define a dp array to store the minimum number of coins needed to make up the amount.
We can iterate through the coins and the amount to update the dp array.
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.

- Initialize a dp array with length amount + 1 and set dp[0] to 0.
- Iterate through the coins and the amount to update the dp array.
- Return dp[amount] if it is less than or equal to the amount, otherwise return -1.

Time complexity: O(n * m)
- n is the amount.
- m is the number of coins.
- We iterate through the coins and the amount to update the dp array.

Space complexity: O(n)
- We use a dp array of length amount + 1 to store the minimum number of coins needed to make up each amount.
"""

from typing import List


class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1] * (amount + 1)
dp[0] = 0 # Base case

for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)

return dp[amount] if dp[amount] != amount + 1 else -1
39 changes: 39 additions & 0 deletions decode-ways/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
91. Decode Ways
https://leetcode.com/problems/decode-ways/

Solution: Dynamic Programming
We can use dynamic programming to count the number of ways to decode the input string.
We can define a dp array to store the number of ways to decode the string up to the current index.
We can iterate through the input string and update the dp array based on the current character and the previous characters.
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.

- Initialize a dp array with length n + 1 and set dp[0] to 1.
- Iterate through the input string starting from the first character.
- Update dp[i] based on the current character and the previous characters.
- Return dp[n].

Time complexity: O(n)
- We iterate through the input string once.

Space complexity: O(n)
- We use a dp array of length n + 1 to store the number of ways to decode the string up to each index.
"""


class Solution:
def numDecodings(self, s: str) -> int:
if not s:
return 0

n = len(s)
dp = [0] * (n + 1)
dp[0] = 1

for i in range(1, n + 1):
if s[i - 1] != "0":
dp[i] += dp[i - 1]
if i > 1 and "10" <= s[i - 2 : i] <= "26":
dp[i] += dp[i - 2]

return dp[n]
40 changes: 40 additions & 0 deletions maximum-product-subarray/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
152. Maximum Product Subarray
https://leetcode.com/problems/maximum-product-subarray/

Solution: Dynamic Programming
To solve this problem, we need to keep track of the maximum product and minimum product so far.
The minimum product can become the maximum product when multiplied by a negative number.

- Initialize min_so_far and max_so_far to the first element of the input array.
- Iterate through the input array starting from the second element.
- Update min_so_far and max_so_far with the current element.
- Update max_product with the maximum of max_so_far and max_product.
- Return max_product.

Time complexity: O(n)
- We iterate through the input array once.

Space complexity: O(1)
- We use only a constant amount of space.


"""

from typing import List


class Solution:
def maxProduct(self, nums: List[int]) -> int:
min_so_far = nums[0]
max_so_far = nums[0]
max_product = max_so_far

for i in range(1, len(nums)):
curr = nums[i]
min_so_far, max_so_far = min(
curr, max_so_far * curr, min_so_far * curr
), max(curr, max_so_far * curr, min_so_far * curr)
max_product = max(max_so_far, max_product)

return max_product
39 changes: 39 additions & 0 deletions palindromic-substrings/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
647. Palindromic Substrings
https://leetcode.com/problems/palindromic-substrings/

Solution:
To solve this problem, we can use the expand from center technique.
We can iterate through each character in the string and expand from the center to find palindromic substrings.
We can handle both odd and even length palindromes by expanding from the center and center + 1.
We keep track of the number of palindromic substrings found.
The total number of palindromic substrings is the sum of palindromic substrings found at each character.

Time complexity: O(n^2)
- We iterate through each character in the string.
- For each character, we expand from the center to find palindromic substrings.
- The time complexity is O(n^2) due to the nested loops.

Space complexity: O(1)
- We use constant space to store variables.
- The space complexity is O(1).
"""


class Solution:
def countSubstrings(self, s: str) -> int:
num_pal_strs = 0

def expand_from_center(left, right):
num_pal = 0
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
num_pal += 1
return num_pal

for i in range(len(s)):
num_pal_strs += expand_from_center(i, i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

병현님도 odd 와 even을 나눠서 계산하는 전략으로 하셨군요

10주차 코드 리뷰 할때 헬레나 님도 그렇게 하셨었어서 신기했었네요.
https://github.com/DaleStudy/leetcode-study/pull/167/files#r1667323986

요 전략은 다른 풀이를 참고를 하신건가요? 아니면 하다보니깐 나오신건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 풀이를 참고했습니다. 다른 방법으로 풀 수 있는지 한 번 고민해봐야할 것 같아요.

num_pal_strs += expand_from_center(i, i + 1)

return num_pal_strs
44 changes: 44 additions & 0 deletions word-break/bhyun-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
139. Word Break
https://leetcode.com/problems/word-break/

Solution:
This problem can be solved using a recursive approach with memoization.
We can define a recursive function that takes an index as an argument.
The function checks if the substring from the current index to the end of the string can be broken into words from the dictionary.
We can use memoization to store the results of subproblems to avoid redundant calculations.

- Define a recursive function that takes an index as an argument.
- Check if the substring from the current index to the end of the string can be broken into words from the dictionary.
- Use memoization to store the results of subproblems.
- Return the result of the recursive function.

Time complexity: O(n*m*k)
- n is the length of the input string.
- m is the number of words in the dictionary.
- k is the average length of the words in the dictionary
- The time complexity is O(n*m*k) due to the nested loops.

Space complexity: O(n)
- We use memoization to store the results of subproblems.
- The space complexity is O(n) to store the results of subproblems.
"""

from functools import cache
from typing import List


class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@cache
def dp(i):
if i < 0:
return True

for word in wordDict:
if s[i - len(word) + 1 : i + 1] == word and dp(i - len(word)):
return True

return False

return dp(len(s) - 1)