From 830ae423435efcafc443aaadde3b40bb545891d1 Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 15 Jul 2024 16:55:30 -0400 Subject: [PATCH 1/5] solve: max sub array --- maximum-subarray/evan.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 maximum-subarray/evan.py diff --git a/maximum-subarray/evan.py b/maximum-subarray/evan.py new file mode 100644 index 000000000..f60fc07fe --- /dev/null +++ b/maximum-subarray/evan.py @@ -0,0 +1,13 @@ +from typing import List + + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + global_max_sum = nums[0] + local_max_sum = nums[0] + + for num in nums[1:]: + local_max_sum = max(num, local_max_sum + num) + global_max_sum = max(global_max_sum, local_max_sum) + + return global_max_sum From 72d5421acb9b23e9657480d7174199e400d94bc9 Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 17 Jul 2024 15:42:23 -0400 Subject: [PATCH 2/5] solve: longest common subsequence --- longest-common-subsequence/evan.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 longest-common-subsequence/evan.py diff --git a/longest-common-subsequence/evan.py b/longest-common-subsequence/evan.py new file mode 100644 index 000000000..1b2ea7ce5 --- /dev/null +++ b/longest-common-subsequence/evan.py @@ -0,0 +1,18 @@ +class Solution: + def longestCommonSubsequence(self, text1, text2): + text1_length, text2_length = len(text1), len(text2) + # dp[i][j]는 text1의 처음 i 글자와 text2의 처음 j 글자의 LCS 길이를 의미합니다. + dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)] + + for text1_char in range(1, text1_length + 1): + for text2_char in range(1, text2_length + 1): + if text1[text1_char - 1] == text2[text2_char - 1]: + # 두 문자가 같으면 그 이전까지의 LCS 길이에 1을 더한 값으로 현재 위치를 갱신합니다. + dp[text1_char][text2_char] = dp[text1_char - 1][text2_char - 1] + 1 + else: + # 두 문자가 다르면 이전까지의 최대 LCS 길이 중 더 큰 값을 현재 위치에 저장합니다. + dp[text1_char][text2_char] = max( + dp[text1_char - 1][text2_char], dp[text1_char][text2_char - 1] + ) + + return dp[text1_length][text2_length] From 059040a85bd679a2ccd4c0c5b64ecca52a4c833f Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 17 Jul 2024 16:03:42 -0400 Subject: [PATCH 3/5] solve: longest increasing subsequence --- longest-increasing-subsequence/evan.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 longest-increasing-subsequence/evan.py diff --git a/longest-increasing-subsequence/evan.py b/longest-increasing-subsequence/evan.py new file mode 100644 index 000000000..abea073e0 --- /dev/null +++ b/longest-increasing-subsequence/evan.py @@ -0,0 +1,19 @@ +import bisect +from typing import List + + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + lis = [] + + for num in nums: + # num이 왼쪽에 삽입될 수 있는 위치 + position = bisect.bisect_left(lis, num) + + # num이 lis에 있는 모든 요소 보다 크다 + if position == len(lis): + lis.append(num) + else: + lis[position] = num + + return len(lis) From 295583a3ebdfcdce5a3fdd7f887d8b1ac0a357d5 Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 22 Jul 2024 08:30:54 -0400 Subject: [PATCH 4/5] solve: jump game --- jump-game/evan.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 jump-game/evan.py diff --git a/jump-game/evan.py b/jump-game/evan.py new file mode 100644 index 000000000..40aa272c2 --- /dev/null +++ b/jump-game/evan.py @@ -0,0 +1,13 @@ +def canJump(nums): + n = len(nums) + dp = [False] * n + dp[0] = True + + for i in range(n): + if dp[i]: + maxJump = min(i + nums[i], n - 1) + + for j in range(i + 1, maxJump + 1): + dp[j] = True + + return dp[n - 1] From 00312e0055c7743dc111fd0b996ca18f1c01c698 Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 22 Jul 2024 20:16:29 -0400 Subject: [PATCH 5/5] solve: unique paths --- unique-paths/evan.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 unique-paths/evan.py diff --git a/unique-paths/evan.py b/unique-paths/evan.py new file mode 100644 index 000000000..a41adb0ac --- /dev/null +++ b/unique-paths/evan.py @@ -0,0 +1,10 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + dp = [[1] * n for _ in range(m)] + + # Iterate over the grid starting from 1,1 to fill the number of unique paths for each cell + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + + return dp[m - 1][n - 1]