-
-
Notifications
You must be signed in to change notification settings - Fork 195
[bhyun-kim, 김병현] Week 12 Solutions #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,29 @@ | ||
""" | ||
55. Jump Game | ||
https://leetcode.com/problems/jump-game/ | ||
|
||
Solution: | ||
To solve this problem, we can use the greedy approach. | ||
We iterate through the array and keep track of the maximum index we can reach. | ||
If the current index is greater than the maximum index we can reach, we return False. | ||
Otherwise, we update the maximum index we can reach. | ||
If we reach the end of the array, we return True. | ||
|
||
Time complexity: O(n) | ||
- We iterate through each element in the array once. | ||
|
||
Space complexity: O(1) | ||
- We use a constant amount of extra space. | ||
""" | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def canJump(self, nums: List[int]) -> bool: | ||
max_reach = 0 | ||
for i, jump in enumerate(nums): | ||
if i > max_reach: | ||
return False | ||
max_reach = max(max_reach, i + jump) | ||
return max_reach >= len(nums) - 1 |
This file contains hidden or 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,34 @@ | ||
""" | ||
1143. Longest Common Subsequence | ||
https://leetcode.com/problems/longest-common-subsequence | ||
|
||
Solution: | ||
To solve this problem, we can use the dynamic programming approach. | ||
We create a 2D list to store the length of the longest common subsequence of the two strings. | ||
We iterate through the two strings and update the length of the longest common subsequence. | ||
We return the length of the longest common subsequence. | ||
|
||
Time complexity: O(n1 * n2) | ||
- We iterate through each character in the two strings once. | ||
- The time complexity is O(n1 * n2) for updating the length of the longest common subsequence. | ||
|
||
Space complexity: O(n1 * n2) | ||
- We use a 2D list to store the length of the longest common subsequence of the two strings. | ||
""" | ||
|
||
|
||
class Solution: | ||
def longestCommonSubsequence(self, text1: str, text2: str) -> int: | ||
n1 = len(text1) | ||
n2 = len(text2) | ||
dp = [[0 for i in range(n2 + 1)] for j in range(n1 + 1)] | ||
maximum = 0 | ||
for i in range(1, n1 + 1): | ||
for j in range(1, n2 + 1): | ||
if text1[i - 1] == text2[j - 1]: | ||
dp[i][j] = dp[i - 1][j - 1] + 1 | ||
else: | ||
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) | ||
maximum = max(dp[i][j], maximum) | ||
|
||
return maximum |
This file contains hidden or 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,33 @@ | ||
""" | ||
300. Longest Increasing Subsequence | ||
https://leetcode.com/problems/longest-increasing-subsequence/ | ||
|
||
Solution: | ||
To solve this problem, we can use the dynamic programming approach. | ||
We create a list to store the longest increasing subsequence ending at each index. | ||
We iterate through the array and update the longest increasing subsequence ending at each index. | ||
We return the maximum length of the longest increasing subsequence. | ||
|
||
Time complexity: O(n log n) | ||
- We iterate through each element in the array once. | ||
- We use the bisect_left function to find the position to insert the element in the sub list. | ||
- The time complexity is O(n log n) for inserting elements in the sub list. | ||
|
||
Space complexity: O(n) | ||
- We use a list to store the longest increasing subsequence ending at each index. | ||
""" | ||
|
||
from typing import List | ||
from bisect import bisect_left | ||
|
||
|
||
class Solution: | ||
def lengthOfLIS(self, nums: List[int]) -> int: | ||
sub = [] | ||
for num in nums: | ||
pos = bisect_left(sub, num) | ||
if pos < len(sub): | ||
sub[pos] = num | ||
else: | ||
sub.append(num) | ||
return len(sub) |
This file contains hidden or 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,31 @@ | ||
""" | ||
53. Maximum Subarray | ||
https://leetcode.com/problems/maximum-subarray | ||
|
||
Solution: | ||
To solve this problem, we can use the Kadane's algorithm. | ||
We keep track of the current sum and the maximum sum. | ||
We iterate through the array and update the current sum and maximum sum. | ||
If the current sum is greater than the maximum sum, we update the maximum sum. | ||
We return the maximum sum at the end. | ||
|
||
Time complexity: O(n) | ||
- We iterate through each element in the array once. | ||
|
||
Space complexity: O(1) | ||
- We use a constant amount of extra space. | ||
""" | ||
|
||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
current_sum = max_sum = nums[0] | ||
|
||
for num in nums[1:]: | ||
current_sum = max(num, current_sum + num) | ||
max_sum = max(max_sum, current_sum) | ||
|
||
return max_sum |
This file contains hidden or 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,35 @@ | ||
""" | ||
62. Unique Paths | ||
https://leetcode.com/problems/unique-paths/ | ||
|
||
Solution: | ||
To solve this problem, we can use the combinatorics approach. | ||
We can calculate the number of unique paths using the formula C(m+n-2, m-1). | ||
We can create a helper function to calculate the factorial of a number. | ||
We calculate the numerator and denominator separately and return the result. | ||
|
||
Time complexity: O(m+n) | ||
- We calculate the factorial of m+n-2, m-1, and n-1. | ||
- The time complexity is O(m+n) for calculating the factorials. | ||
|
||
Space complexity: O(1) | ||
- We use a constant amount of extra space. | ||
""" | ||
|
||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
def factorial(num): | ||
previous = 1 | ||
current = 1 | ||
for i in range(2, num + 1): | ||
current = previous * i | ||
previous = current | ||
return current | ||
|
||
max_num = m + n - 2 | ||
numerator = factorial(max_num) | ||
factorial_m_minus_1 = factorial(m - 1) | ||
factorial_n_minus_1 = factorial(n - 1) | ||
result = numerator // (factorial_m_minus_1 * factorial_n_minus_1) | ||
return result |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python은 이분탐색도 지원해주는군요..?
문제풀이 하는 것이니만큼 이분탐색을 구현해보는 것도 좋은 연습이 될 것 같아요. :)