Skip to content

Commit 274370b

Browse files
authored
Merge pull request DaleStudy#194 from bhyun-kim/main
[bhyun-kim, 김병현] Week 12 Solutions
2 parents 12618c1 + 98b203d commit 274370b

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

jump-game/bhyun-kim.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
55. Jump Game
3+
https://leetcode.com/problems/jump-game/
4+
5+
Solution:
6+
To solve this problem, we can use the greedy approach.
7+
We iterate through the array and keep track of the maximum index we can reach.
8+
If the current index is greater than the maximum index we can reach, we return False.
9+
Otherwise, we update the maximum index we can reach.
10+
If we reach the end of the array, we return True.
11+
12+
Time complexity: O(n)
13+
- We iterate through each element in the array once.
14+
15+
Space complexity: O(1)
16+
- We use a constant amount of extra space.
17+
"""
18+
19+
from typing import List
20+
21+
22+
class Solution:
23+
def canJump(self, nums: List[int]) -> bool:
24+
max_reach = 0
25+
for i, jump in enumerate(nums):
26+
if i > max_reach:
27+
return False
28+
max_reach = max(max_reach, i + jump)
29+
return max_reach >= len(nums) - 1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
1143. Longest Common Subsequence
3+
https://leetcode.com/problems/longest-common-subsequence
4+
5+
Solution:
6+
To solve this problem, we can use the dynamic programming approach.
7+
We create a 2D list to store the length of the longest common subsequence of the two strings.
8+
We iterate through the two strings and update the length of the longest common subsequence.
9+
We return the length of the longest common subsequence.
10+
11+
Time complexity: O(n1 * n2)
12+
- We iterate through each character in the two strings once.
13+
- The time complexity is O(n1 * n2) for updating the length of the longest common subsequence.
14+
15+
Space complexity: O(n1 * n2)
16+
- We use a 2D list to store the length of the longest common subsequence of the two strings.
17+
"""
18+
19+
20+
class Solution:
21+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
22+
n1 = len(text1)
23+
n2 = len(text2)
24+
dp = [[0 for i in range(n2 + 1)] for j in range(n1 + 1)]
25+
maximum = 0
26+
for i in range(1, n1 + 1):
27+
for j in range(1, n2 + 1):
28+
if text1[i - 1] == text2[j - 1]:
29+
dp[i][j] = dp[i - 1][j - 1] + 1
30+
else:
31+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
32+
maximum = max(dp[i][j], maximum)
33+
34+
return maximum
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
300. Longest Increasing Subsequence
3+
https://leetcode.com/problems/longest-increasing-subsequence/
4+
5+
Solution:
6+
To solve this problem, we can use the dynamic programming approach.
7+
We create a list to store the longest increasing subsequence ending at each index.
8+
We iterate through the array and update the longest increasing subsequence ending at each index.
9+
We return the maximum length of the longest increasing subsequence.
10+
11+
Time complexity: O(n log n)
12+
- We iterate through each element in the array once.
13+
- We use the bisect_left function to find the position to insert the element in the sub list.
14+
- The time complexity is O(n log n) for inserting elements in the sub list.
15+
16+
Space complexity: O(n)
17+
- We use a list to store the longest increasing subsequence ending at each index.
18+
"""
19+
20+
from typing import List
21+
from bisect import bisect_left
22+
23+
24+
class Solution:
25+
def lengthOfLIS(self, nums: List[int]) -> int:
26+
sub = []
27+
for num in nums:
28+
pos = bisect_left(sub, num)
29+
if pos < len(sub):
30+
sub[pos] = num
31+
else:
32+
sub.append(num)
33+
return len(sub)

maximum-subarray/bhyun-kim.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
53. Maximum Subarray
3+
https://leetcode.com/problems/maximum-subarray
4+
5+
Solution:
6+
To solve this problem, we can use the Kadane's algorithm.
7+
We keep track of the current sum and the maximum sum.
8+
We iterate through the array and update the current sum and maximum sum.
9+
If the current sum is greater than the maximum sum, we update the maximum sum.
10+
We return the maximum sum at the end.
11+
12+
Time complexity: O(n)
13+
- We iterate through each element in the array once.
14+
15+
Space complexity: O(1)
16+
- We use a constant amount of extra space.
17+
"""
18+
19+
20+
from typing import List
21+
22+
23+
class Solution:
24+
def maxSubArray(self, nums: List[int]) -> int:
25+
current_sum = max_sum = nums[0]
26+
27+
for num in nums[1:]:
28+
current_sum = max(num, current_sum + num)
29+
max_sum = max(max_sum, current_sum)
30+
31+
return max_sum

unique-paths/bhyun-kim.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
62. Unique Paths
3+
https://leetcode.com/problems/unique-paths/
4+
5+
Solution:
6+
To solve this problem, we can use the combinatorics approach.
7+
We can calculate the number of unique paths using the formula C(m+n-2, m-1).
8+
We can create a helper function to calculate the factorial of a number.
9+
We calculate the numerator and denominator separately and return the result.
10+
11+
Time complexity: O(m+n)
12+
- We calculate the factorial of m+n-2, m-1, and n-1.
13+
- The time complexity is O(m+n) for calculating the factorials.
14+
15+
Space complexity: O(1)
16+
- We use a constant amount of extra space.
17+
"""
18+
19+
20+
class Solution:
21+
def uniquePaths(self, m: int, n: int) -> int:
22+
def factorial(num):
23+
previous = 1
24+
current = 1
25+
for i in range(2, num + 1):
26+
current = previous * i
27+
previous = current
28+
return current
29+
30+
max_num = m + n - 2
31+
numerator = factorial(max_num)
32+
factorial_m_minus_1 = factorial(m - 1)
33+
factorial_n_minus_1 = factorial(n - 1)
34+
result = numerator // (factorial_m_minus_1 * factorial_n_minus_1)
35+
return result

0 commit comments

Comments
 (0)