From 23449252313deff59601cc11c38952b03b26ec48 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 1 May 2024 14:17:36 -0500 Subject: [PATCH 1/6] [bhyun-kim] Solution for week1 assignments --- best-time-to-buy-and-sell-stock/bhyun-kim.py | 42 ++++++++++++++++++++ contains-duplicate/bhyun-kim.py | 33 +++++++++++++++ two-sum/bhyun-kim.py | 41 +++++++++++++++++++ valid-anagram/bhyun-kim.py | 33 +++++++++++++++ valid-palindrome/bhyun-kim.py | 36 +++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/bhyun-kim.py create mode 100644 contains-duplicate/bhyun-kim.py create mode 100644 two-sum/bhyun-kim.py create mode 100644 valid-anagram/bhyun-kim.py create mode 100644 valid-palindrome/bhyun-kim.py diff --git a/best-time-to-buy-and-sell-stock/bhyun-kim.py b/best-time-to-buy-and-sell-stock/bhyun-kim.py new file mode 100644 index 000000000..11643e282 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/bhyun-kim.py @@ -0,0 +1,42 @@ +""" +Solution + +Algorithm: + 1. Iterate through the list in reverse. + 2. Keep track of the maximum value seen so far. + 3. Calculate the profit by subtracting the current value from the maximum value. + 4. Update the profit if it is greater than the current profit. + +Time complexity: O(n) +Space complexity: O(1) +""" + + +from typing import List + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + prev_max = 0 + + for i in reversed(range(len(prices)-1)): + prev_max = max(prev_max, prices[i+1]) + profit = max(profit, prev_max-prices[i]) + + return profit + + + +def main(): + test_cases = [ + [[7,1,5,3,6,4], 5], + [[7,6,4,3,1], 0] + ] + s = Solution() + + for test_case in test_cases: + prices_input, expected = test_case + assert s.maxProfit(prices_input) == expected + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/contains-duplicate/bhyun-kim.py b/contains-duplicate/bhyun-kim.py new file mode 100644 index 000000000..b04260d1f --- /dev/null +++ b/contains-duplicate/bhyun-kim.py @@ -0,0 +1,33 @@ +""" +Solution + +Algorithm: + 1. Create a set from the list. + 2. If the length of the set is not equal to the length of the list, return True. + 3. Otherwise, return False. + +Time complexity: O(n) +Space complexity: O(n) +""" + + +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(set(nums)) != len(nums) + +def main(): + test_cases = [ + [[1,2,3,1], True,] + [[1,2,3,4], False], + [[1,1,1,3,3,4,3,2,4,2], True] + ] + s = Solution() + + for test_case in test_cases: + nums_input, expected = test_case + assert s.containsDuplicate(nums_input) == expected + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/two-sum/bhyun-kim.py b/two-sum/bhyun-kim.py new file mode 100644 index 000000000..bfd80813a --- /dev/null +++ b/two-sum/bhyun-kim.py @@ -0,0 +1,41 @@ +""" +Solution + +Algorithm: + 1. Create a hashmap to store the index of each element. + 2. Iterate through the list. + 3. Check if the remaining value is in the hashmap. + 4. If it is, return the current index and the index of the remaining value in the hashmap. + +Time complexity: O(n) +Space complexity: O(n) +""" + +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + hashmap = {} + + for i in range(len(nums)): + remaining = target - nums[i] + if remaining in hashmap: + return [i, hashmap[remaining]] + + hashmap[nums[i]] = i + +def main(): + test_cases = [ + [2,7,11,15], 9, [0,1], + [3,2,4], 6, [1,2], + [3,3], 6, [0,1] + ] + s = Solution() + + for test_case in test_cases: + nums_input, target_input, expected = test_case + assert sorted(s.twoSum(nums_input, target_input)) == expected + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/valid-anagram/bhyun-kim.py b/valid-anagram/bhyun-kim.py new file mode 100644 index 000000000..2dcbcabc8 --- /dev/null +++ b/valid-anagram/bhyun-kim.py @@ -0,0 +1,33 @@ +""" +Solution + +Algorithm: + 1. Sort the strings and compare them. + 2. If they are equal, return True. Otherwise, return False. + +Time complexity: O(nlogn) +Space complexity: O(1) +""" + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + + if sorted(s) == sorted(t): + return True + else: + return False + + +def main(): + test_cases = [ + ["anagram", "nagaram", True], + ["rat", "car", False] + ] + s = Solution() + + for test_case in test_cases: + s_input, t_input, expected = test_case + assert s.isAnagram(s_input, t_input) == expected + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py new file mode 100644 index 000000000..84f2a5cf4 --- /dev/null +++ b/valid-palindrome/bhyun-kim.py @@ -0,0 +1,36 @@ +""" +Solution: + +Algorithm: + 1. Convert the string to lowercase. + 2. Remove all non-alphanumeric characters. + 3. Check if the string is equal to its reverse. + +Time complexity: O(n) +Space complexity: O(n) + +""" + + +class Solution: + def isPalindrome(self, s: str) -> bool: + s = s.lower() + s = list([c for c in s + if c.isalpha() or c.isnumeric()]) + s = "".join(s) + return s == s[::-1] + +def main(): + test_cases = [ + ["A man, a plan, a canal: Panama", True], + ["race a car", False], + [" ", True] + ] + s = Solution() + + for test_case in test_cases: + s_input, expected = test_case + assert s.isPalindrome(s_input) == expected + +if __name__ == '__main__': + main() \ No newline at end of file From 23c97c0d034f3a905bfe63f60dffae5915073336 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 1 May 2024 14:31:35 -0500 Subject: [PATCH 2/6] Add line breaks --- best-time-to-buy-and-sell-stock/bhyun-kim.py | 3 ++- contains-duplicate/bhyun-kim.py | 3 ++- two-sum/bhyun-kim.py | 3 ++- valid-anagram/bhyun-kim.py | 3 ++- valid-palindrome/bhyun-kim.py | 5 ++--- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/bhyun-kim.py b/best-time-to-buy-and-sell-stock/bhyun-kim.py index 11643e282..ca69df1e6 100644 --- a/best-time-to-buy-and-sell-stock/bhyun-kim.py +++ b/best-time-to-buy-and-sell-stock/bhyun-kim.py @@ -39,4 +39,5 @@ def main(): assert s.maxProfit(prices_input) == expected if __name__ == '__main__': - main() \ No newline at end of file + main() + \ No newline at end of file diff --git a/contains-duplicate/bhyun-kim.py b/contains-duplicate/bhyun-kim.py index b04260d1f..4102d403b 100644 --- a/contains-duplicate/bhyun-kim.py +++ b/contains-duplicate/bhyun-kim.py @@ -30,4 +30,5 @@ def main(): assert s.containsDuplicate(nums_input) == expected if __name__ == '__main__': - main() \ No newline at end of file + main() + \ No newline at end of file diff --git a/two-sum/bhyun-kim.py b/two-sum/bhyun-kim.py index bfd80813a..596980fbe 100644 --- a/two-sum/bhyun-kim.py +++ b/two-sum/bhyun-kim.py @@ -38,4 +38,5 @@ def main(): assert sorted(s.twoSum(nums_input, target_input)) == expected if __name__ == '__main__': - main() \ No newline at end of file + main() + \ No newline at end of file diff --git a/valid-anagram/bhyun-kim.py b/valid-anagram/bhyun-kim.py index 2dcbcabc8..dd95d5cab 100644 --- a/valid-anagram/bhyun-kim.py +++ b/valid-anagram/bhyun-kim.py @@ -30,4 +30,5 @@ def main(): assert s.isAnagram(s_input, t_input) == expected if __name__ == '__main__': - main() \ No newline at end of file + main() + \ No newline at end of file diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py index 84f2a5cf4..4a3afde77 100644 --- a/valid-palindrome/bhyun-kim.py +++ b/valid-palindrome/bhyun-kim.py @@ -7,8 +7,7 @@ 3. Check if the string is equal to its reverse. Time complexity: O(n) -Space complexity: O(n) - +Space complexity: O(n) """ @@ -33,4 +32,4 @@ def main(): assert s.isPalindrome(s_input) == expected if __name__ == '__main__': - main() \ No newline at end of file + main() From 7246cf74deebf6bdc2f79ec29ff63b2c05bed64e Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 1 May 2024 19:29:54 -0500 Subject: [PATCH 3/6] Add line break and remove unnecessary string conversion --- best-time-to-buy-and-sell-stock/bhyun-kim.py | 19 ++++++++----------- contains-duplicate/bhyun-kim.py | 16 ++++++++++------ two-sum/bhyun-kim.py | 15 ++++++--------- valid-anagram/bhyun-kim.py | 13 +++++-------- valid-palindrome/bhyun-kim.py | 13 +++++++------ 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/bhyun-kim.py b/best-time-to-buy-and-sell-stock/bhyun-kim.py index ca69df1e6..585a7f9f4 100644 --- a/best-time-to-buy-and-sell-stock/bhyun-kim.py +++ b/best-time-to-buy-and-sell-stock/bhyun-kim.py @@ -14,30 +14,27 @@ from typing import List + class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 prev_max = 0 - for i in reversed(range(len(prices)-1)): - prev_max = max(prev_max, prices[i+1]) - profit = max(profit, prev_max-prices[i]) + for i in reversed(range(len(prices) - 1)): + prev_max = max(prev_max, prices[i + 1]) + profit = max(profit, prev_max - prices[i]) return profit - def main(): - test_cases = [ - [[7,1,5,3,6,4], 5], - [[7,6,4,3,1], 0] - ] + test_cases = [[[7, 1, 5, 3, 6, 4], 5], [[7, 6, 4, 3, 1], 0]] s = Solution() - + for test_case in test_cases: prices_input, expected = test_case assert s.maxProfit(prices_input) == expected -if __name__ == '__main__': + +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/contains-duplicate/bhyun-kim.py b/contains-duplicate/bhyun-kim.py index 4102d403b..3ee6dfd89 100644 --- a/contains-duplicate/bhyun-kim.py +++ b/contains-duplicate/bhyun-kim.py @@ -13,22 +13,26 @@ from typing import List + class Solution: def containsDuplicate(self, nums: List[int]) -> bool: return len(set(nums)) != len(nums) + def main(): test_cases = [ - [[1,2,3,1], True,] - [[1,2,3,4], False], - [[1,1,1,3,3,4,3,2,4,2], True] + [ + [1, 2, 3, 1], + True, + ][[1, 2, 3, 4], False], + [[1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True], ] s = Solution() - + for test_case in test_cases: nums_input, expected = test_case assert s.containsDuplicate(nums_input) == expected -if __name__ == '__main__': + +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/two-sum/bhyun-kim.py b/two-sum/bhyun-kim.py index 596980fbe..824c30140 100644 --- a/two-sum/bhyun-kim.py +++ b/two-sum/bhyun-kim.py @@ -13,9 +13,9 @@ from typing import List + class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - hashmap = {} for i in range(len(nums)): @@ -25,18 +25,15 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: hashmap[nums[i]] = i + def main(): - test_cases = [ - [2,7,11,15], 9, [0,1], - [3,2,4], 6, [1,2], - [3,3], 6, [0,1] - ] + test_cases = [[2, 7, 11, 15], 9, [0, 1], [3, 2, 4], 6, [1, 2], [3, 3], 6, [0, 1]] s = Solution() - + for test_case in test_cases: nums_input, target_input, expected = test_case assert sorted(s.twoSum(nums_input, target_input)) == expected -if __name__ == '__main__': + +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/valid-anagram/bhyun-kim.py b/valid-anagram/bhyun-kim.py index dd95d5cab..61a352af6 100644 --- a/valid-anagram/bhyun-kim.py +++ b/valid-anagram/bhyun-kim.py @@ -9,9 +9,9 @@ Space complexity: O(1) """ + class Solution: def isAnagram(self, s: str, t: str) -> bool: - if sorted(s) == sorted(t): return True else: @@ -19,16 +19,13 @@ def isAnagram(self, s: str, t: str) -> bool: def main(): - test_cases = [ - ["anagram", "nagaram", True], - ["rat", "car", False] - ] + test_cases = [["anagram", "nagaram", True], ["rat", "car", False]] s = Solution() - + for test_case in test_cases: s_input, t_input, expected = test_case assert s.isAnagram(s_input, t_input) == expected -if __name__ == '__main__': + +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py index 4a3afde77..53746deb2 100644 --- a/valid-palindrome/bhyun-kim.py +++ b/valid-palindrome/bhyun-kim.py @@ -14,22 +14,23 @@ class Solution: def isPalindrome(self, s: str) -> bool: s = s.lower() - s = list([c for c in s - if c.isalpha() or c.isnumeric()]) - s = "".join(s) + s = [c for c in s if c.isalpha() or c.isnumeric()] return s == s[::-1] + def main(): test_cases = [ ["A man, a plan, a canal: Panama", True], ["race a car", False], - [" ", True] + [" ", True], ] s = Solution() - + for test_case in test_cases: s_input, expected = test_case assert s.isPalindrome(s_input) == expected -if __name__ == '__main__': + +if __name__ == "__main__": main() + \ No newline at end of file From a6423fe2b5661ffaf853f3805f5400414a65088d Mon Sep 17 00:00:00 2001 From: bhyun-kim <94029750+bhyun-kim@users.noreply.github.com> Date: Wed, 1 May 2024 19:48:50 -0500 Subject: [PATCH 4/6] Update bhyun-kim.py --- valid-palindrome/bhyun-kim.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py index 53746deb2..b5fcfba41 100644 --- a/valid-palindrome/bhyun-kim.py +++ b/valid-palindrome/bhyun-kim.py @@ -33,4 +33,4 @@ def main(): if __name__ == "__main__": main() - \ No newline at end of file + From 4e7da32474cf362d1605003855a21ce4442b5996 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 1 May 2024 19:50:02 -0500 Subject: [PATCH 5/6] Fix line break --- valid-palindrome/bhyun-kim.py | 1 - 1 file changed, 1 deletion(-) diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py index 53746deb2..905818310 100644 --- a/valid-palindrome/bhyun-kim.py +++ b/valid-palindrome/bhyun-kim.py @@ -33,4 +33,3 @@ def main(): if __name__ == "__main__": main() - \ No newline at end of file From 7fc889647c8fa0bc37136aab8dcd40ce313424ec Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Thu, 2 May 2024 12:21:55 -0500 Subject: [PATCH 6/6] Shorten anagram solution --- valid-anagram/bhyun-kim.py | 5 +---- valid-palindrome/bhyun-kim.py | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/valid-anagram/bhyun-kim.py b/valid-anagram/bhyun-kim.py index 61a352af6..1d9098d77 100644 --- a/valid-anagram/bhyun-kim.py +++ b/valid-anagram/bhyun-kim.py @@ -12,10 +12,7 @@ class Solution: def isAnagram(self, s: str, t: str) -> bool: - if sorted(s) == sorted(t): - return True - else: - return False + return sorted(s) == sorted(t) def main(): diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py index eb8649ca0..905818310 100644 --- a/valid-palindrome/bhyun-kim.py +++ b/valid-palindrome/bhyun-kim.py @@ -33,4 +33,3 @@ def main(): if __name__ == "__main__": main() -