forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |