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..585a7f9f4 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/bhyun-kim.py @@ -0,0 +1,40 @@ +""" +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() diff --git a/contains-duplicate/bhyun-kim.py b/contains-duplicate/bhyun-kim.py new file mode 100644 index 000000000..3ee6dfd89 --- /dev/null +++ b/contains-duplicate/bhyun-kim.py @@ -0,0 +1,38 @@ +""" +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() diff --git a/two-sum/bhyun-kim.py b/two-sum/bhyun-kim.py new file mode 100644 index 000000000..824c30140 --- /dev/null +++ b/two-sum/bhyun-kim.py @@ -0,0 +1,39 @@ +""" +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() diff --git a/valid-anagram/bhyun-kim.py b/valid-anagram/bhyun-kim.py new file mode 100644 index 000000000..1d9098d77 --- /dev/null +++ b/valid-anagram/bhyun-kim.py @@ -0,0 +1,28 @@ +""" +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: + return sorted(s) == sorted(t) + + +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() diff --git a/valid-palindrome/bhyun-kim.py b/valid-palindrome/bhyun-kim.py new file mode 100644 index 000000000..905818310 --- /dev/null +++ b/valid-palindrome/bhyun-kim.py @@ -0,0 +1,35 @@ +""" +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 = [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], + ] + s = Solution() + + for test_case in test_cases: + s_input, expected = test_case + assert s.isPalindrome(s_input) == expected + + +if __name__ == "__main__": + main()