Skip to content

[SAM] Week 12 solutions #188

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 5 commits into from
Jul 23, 2024
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
10 changes: 10 additions & 0 deletions jump-game/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TC : O(n)
# SC : O(1)
class Solution:
def canJump(self, nums):
reachable = 0
for i in range(len(nums)):
if i > reachable:
return False
reachable = max(reachable, i + nums[i])
return True
13 changes: 13 additions & 0 deletions longest-common-subsequence/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# m is the length of text1 and n is the length of text2
# TC : O(m * n)
# SC : O(m * n)
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
for r in range(1, len(text1) + 1):
for c in range(1, len(text2) + 1):
if text1[r - 1] == text2[c - 1]:
dp[r][c] = 1 + dp[r - 1][c - 1]
else:
dp[r][c] = max(dp[r - 1][c], dp[r][c - 1])
return dp[-1][-1]
10 changes: 10 additions & 0 deletions longest-increasing-subsequence/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TC : O(n^2)
# SC : O(n)
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for cur in range(1, len(nums)):
for pre in range(cur):
if nums[pre] < nums[cur]:
dp[cur] = max(1 + dp[pre], dp[cur])
return max(dp)
15 changes: 15 additions & 0 deletions maximum-subarray/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# TC : O(n)
# SC : O(1)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
res = nums[0]
total = 0

for n in nums:
if total < 0:
total = 0

total += n
res = max(res, total)

return res
5 changes: 5 additions & 0 deletions unique-paths/samthekorean.py
Copy link
Contributor

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์€ ํ™•์‹คํžˆ ์ˆ˜ํ•™์ชฝ ๊ธฐ๋ณธ ์œ ํ‹ธ์ด ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š”๊ฒƒ ๊ฐ™๋„ค์š”

https://docs.python.org/3/library/math.html#math.comb

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TC : O(n)
# SC : O(1)
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return math.comb(m + n - 2, n - 1)