From a8123140c93c2e58a55386a9d4d8877c44a09686 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Tue, 16 Jul 2024 17:48:55 -0400 Subject: [PATCH 1/5] solve : longest increasing subsequence --- longest-increasing-subsequence/samthekorean.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 longest-increasing-subsequence/samthekorean.py diff --git a/longest-increasing-subsequence/samthekorean.py b/longest-increasing-subsequence/samthekorean.py new file mode 100644 index 000000000..f3977fa8e --- /dev/null +++ b/longest-increasing-subsequence/samthekorean.py @@ -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) From ca162fd31f1c250866a75812ef91da5ecd42dca1 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Wed, 17 Jul 2024 12:32:23 -0400 Subject: [PATCH 2/5] solve : unique path --- unique-paths/samthekorean.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 unique-paths/samthekorean.py diff --git a/unique-paths/samthekorean.py b/unique-paths/samthekorean.py new file mode 100644 index 000000000..da8ac40fe --- /dev/null +++ b/unique-paths/samthekorean.py @@ -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) From 435e438078facce525c956af16724ff6c48c9c7f Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 19 Jul 2024 14:49:44 -0400 Subject: [PATCH 3/5] solve : jump game --- jump-game/samthekorean.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 jump-game/samthekorean.py diff --git a/jump-game/samthekorean.py b/jump-game/samthekorean.py new file mode 100644 index 000000000..e4496504f --- /dev/null +++ b/jump-game/samthekorean.py @@ -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 From 99ec8871cf5af4cbc4dfe8e73a3d9313da0f8763 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 19 Jul 2024 14:59:37 -0400 Subject: [PATCH 4/5] solve : maximum subarray --- maximum-subarray/samthekorean.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 maximum-subarray/samthekorean.py diff --git a/maximum-subarray/samthekorean.py b/maximum-subarray/samthekorean.py new file mode 100644 index 000000000..b8b90e75e --- /dev/null +++ b/maximum-subarray/samthekorean.py @@ -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 From cf0fc60c3fe38baede0a8296dec3442f2ef4ed63 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Sat, 20 Jul 2024 11:19:23 -0400 Subject: [PATCH 5/5] solve : longest common subsequence --- longest-common-subsequence/samthekorean.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 longest-common-subsequence/samthekorean.py diff --git a/longest-common-subsequence/samthekorean.py b/longest-common-subsequence/samthekorean.py new file mode 100644 index 000000000..5c080f28b --- /dev/null +++ b/longest-common-subsequence/samthekorean.py @@ -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]