Skip to content

Commit fb1b9b7

Browse files
authored
Merge pull request #188 from SamTheKorean/main
[SAM] Week 12 solutions
2 parents b8be309 + cf0fc60 commit fb1b9b7

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

jump-game/samthekorean.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def canJump(self, nums):
5+
reachable = 0
6+
for i in range(len(nums)):
7+
if i > reachable:
8+
return False
9+
reachable = max(reachable, i + nums[i])
10+
return True
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# m is the length of text1 and n is the length of text2
2+
# TC : O(m * n)
3+
# SC : O(m * n)
4+
class Solution:
5+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
6+
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
7+
for r in range(1, len(text1) + 1):
8+
for c in range(1, len(text2) + 1):
9+
if text1[r - 1] == text2[c - 1]:
10+
dp[r][c] = 1 + dp[r - 1][c - 1]
11+
else:
12+
dp[r][c] = max(dp[r - 1][c], dp[r][c - 1])
13+
return dp[-1][-1]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TC : O(n^2)
2+
# SC : O(n)
3+
class Solution:
4+
def lengthOfLIS(self, nums: List[int]) -> int:
5+
dp = [1] * len(nums)
6+
for cur in range(1, len(nums)):
7+
for pre in range(cur):
8+
if nums[pre] < nums[cur]:
9+
dp[cur] = max(1 + dp[pre], dp[cur])
10+
return max(dp)

maximum-subarray/samthekorean.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def maxSubArray(self, nums: List[int]) -> int:
5+
res = nums[0]
6+
total = 0
7+
8+
for n in nums:
9+
if total < 0:
10+
total = 0
11+
12+
total += n
13+
res = max(res, total)
14+
15+
return res

unique-paths/samthekorean.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def uniquePaths(self, m: int, n: int) -> int:
5+
return math.comb(m + n - 2, n - 1)

0 commit comments

Comments
 (0)