From 98b203db07ec937cf2be62d466788a5108194ca6 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Sat, 20 Jul 2024 00:26:40 -0500 Subject: [PATCH] =?UTF-8?q?[bhyun-kim,=20=EA=B9=80=EB=B3=91=ED=98=84]=20We?= =?UTF-8?q?ek=2012=20Solutions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jump-game/bhyun-kim.py | 29 +++++++++++++++++ longest-common-subsequence/bhyun-kim.py | 34 ++++++++++++++++++++ longest-increasing-subsequence/bhyun-kim.py | 33 +++++++++++++++++++ maximum-subarray/bhyun-kim.py | 31 ++++++++++++++++++ unique-paths/bhyun-kim.py | 35 +++++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 jump-game/bhyun-kim.py create mode 100644 longest-common-subsequence/bhyun-kim.py create mode 100644 longest-increasing-subsequence/bhyun-kim.py create mode 100644 maximum-subarray/bhyun-kim.py create mode 100644 unique-paths/bhyun-kim.py diff --git a/jump-game/bhyun-kim.py b/jump-game/bhyun-kim.py new file mode 100644 index 000000000..cd1337535 --- /dev/null +++ b/jump-game/bhyun-kim.py @@ -0,0 +1,29 @@ +""" +55. Jump Game +https://leetcode.com/problems/jump-game/ + +Solution: + To solve this problem, we can use the greedy approach. + We iterate through the array and keep track of the maximum index we can reach. + If the current index is greater than the maximum index we can reach, we return False. + Otherwise, we update the maximum index we can reach. + If we reach the end of the array, we return True. + +Time complexity: O(n) + - We iterate through each element in the array once. + +Space complexity: O(1) + - We use a constant amount of extra space. +""" + +from typing import List + + +class Solution: + def canJump(self, nums: List[int]) -> bool: + max_reach = 0 + for i, jump in enumerate(nums): + if i > max_reach: + return False + max_reach = max(max_reach, i + jump) + return max_reach >= len(nums) - 1 diff --git a/longest-common-subsequence/bhyun-kim.py b/longest-common-subsequence/bhyun-kim.py new file mode 100644 index 000000000..bd1382f12 --- /dev/null +++ b/longest-common-subsequence/bhyun-kim.py @@ -0,0 +1,34 @@ +""" +1143. Longest Common Subsequence +https://leetcode.com/problems/longest-common-subsequence + +Solution: + To solve this problem, we can use the dynamic programming approach. + We create a 2D list to store the length of the longest common subsequence of the two strings. + We iterate through the two strings and update the length of the longest common subsequence. + We return the length of the longest common subsequence. + +Time complexity: O(n1 * n2) + - We iterate through each character in the two strings once. + - The time complexity is O(n1 * n2) for updating the length of the longest common subsequence. + +Space complexity: O(n1 * n2) + - We use a 2D list to store the length of the longest common subsequence of the two strings. +""" + + +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + n1 = len(text1) + n2 = len(text2) + dp = [[0 for i in range(n2 + 1)] for j in range(n1 + 1)] + maximum = 0 + for i in range(1, n1 + 1): + for j in range(1, n2 + 1): + if text1[i - 1] == text2[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + 1 + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + maximum = max(dp[i][j], maximum) + + return maximum diff --git a/longest-increasing-subsequence/bhyun-kim.py b/longest-increasing-subsequence/bhyun-kim.py new file mode 100644 index 000000000..ddd29cecf --- /dev/null +++ b/longest-increasing-subsequence/bhyun-kim.py @@ -0,0 +1,33 @@ +""" +300. Longest Increasing Subsequence +https://leetcode.com/problems/longest-increasing-subsequence/ + +Solution: + To solve this problem, we can use the dynamic programming approach. + We create a list to store the longest increasing subsequence ending at each index. + We iterate through the array and update the longest increasing subsequence ending at each index. + We return the maximum length of the longest increasing subsequence. + +Time complexity: O(n log n) + - We iterate through each element in the array once. + - We use the bisect_left function to find the position to insert the element in the sub list. + - The time complexity is O(n log n) for inserting elements in the sub list. + +Space complexity: O(n) + - We use a list to store the longest increasing subsequence ending at each index. +""" + +from typing import List +from bisect import bisect_left + + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + sub = [] + for num in nums: + pos = bisect_left(sub, num) + if pos < len(sub): + sub[pos] = num + else: + sub.append(num) + return len(sub) diff --git a/maximum-subarray/bhyun-kim.py b/maximum-subarray/bhyun-kim.py new file mode 100644 index 000000000..a2ab76dc2 --- /dev/null +++ b/maximum-subarray/bhyun-kim.py @@ -0,0 +1,31 @@ +""" +53. Maximum Subarray +https://leetcode.com/problems/maximum-subarray + +Solution: + To solve this problem, we can use the Kadane's algorithm. + We keep track of the current sum and the maximum sum. + We iterate through the array and update the current sum and maximum sum. + If the current sum is greater than the maximum sum, we update the maximum sum. + We return the maximum sum at the end. + +Time complexity: O(n) + - We iterate through each element in the array once. + +Space complexity: O(1) + - We use a constant amount of extra space. +""" + + +from typing import List + + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + current_sum = max_sum = nums[0] + + for num in nums[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(max_sum, current_sum) + + return max_sum diff --git a/unique-paths/bhyun-kim.py b/unique-paths/bhyun-kim.py new file mode 100644 index 000000000..e505cf51d --- /dev/null +++ b/unique-paths/bhyun-kim.py @@ -0,0 +1,35 @@ +""" +62. Unique Paths +https://leetcode.com/problems/unique-paths/ + +Solution: + To solve this problem, we can use the combinatorics approach. + We can calculate the number of unique paths using the formula C(m+n-2, m-1). + We can create a helper function to calculate the factorial of a number. + We calculate the numerator and denominator separately and return the result. + +Time complexity: O(m+n) + - We calculate the factorial of m+n-2, m-1, and n-1. + - The time complexity is O(m+n) for calculating the factorials. + +Space complexity: O(1) + - We use a constant amount of extra space. +""" + + +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + def factorial(num): + previous = 1 + current = 1 + for i in range(2, num + 1): + current = previous * i + previous = current + return current + + max_num = m + n - 2 + numerator = factorial(max_num) + factorial_m_minus_1 = factorial(m - 1) + factorial_n_minus_1 = factorial(n - 1) + result = numerator // (factorial_m_minus_1 * factorial_n_minus_1) + return result