From b61d5b06a65ed74c41e306f8fc6a6cead4c7990a Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 14 Dec 2024 11:11:51 +0900 Subject: [PATCH 1/5] add solution containsDuplicate --- contains-duplicate/JonghunLee.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 contains-duplicate/JonghunLee.py diff --git a/contains-duplicate/JonghunLee.py b/contains-duplicate/JonghunLee.py new file mode 100644 index 000000000..ecd4489fc --- /dev/null +++ b/contains-duplicate/JonghunLee.py @@ -0,0 +1,27 @@ + +# Time complexity: O(n) +# The process of traversing a list to generate a set is proportional to the length of the input list. +# Space complexity: O(n) +# The size of the set for storing deduplicated elements is proportional to the length of the input list. + +class Solution: + def containsDuplicate(self, nums: list[int]) -> bool: + list_len = len(nums) + set_len = len(set(nums)) + + return list_len != set_len + +if __name__ == "__main__": + solution = Solution() + + # test case + test_string = [ + [1,2,3,1], # True + [1,2,3,4], # False + [1,1,1,3,3,4,3,2,4,2], # True + ] + + for index, test in enumerate(test_string): + print(f"start {index} test") + print(f"input : {test}") + print(f"Is valid palindrome ? {solution.containsDuplicate(test)}\n") From 80a4899df0c520d6a25a5ee2ea843b34c193e667 Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 14 Dec 2024 11:12:08 +0900 Subject: [PATCH 2/5] add solution valid palindrome --- valid-palindrome/JonghunLee.py | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 valid-palindrome/JonghunLee.py diff --git a/valid-palindrome/JonghunLee.py b/valid-palindrome/JonghunLee.py new file mode 100644 index 000000000..5a3cd64a8 --- /dev/null +++ b/valid-palindrome/JonghunLee.py @@ -0,0 +1,51 @@ +import re + + +# Time Complexity O(n) +# - Both normal preprocessing and two-pointer comparisons have time complexity proportional to the length of the input string. +# Space Complexity O(n) +# - The space of the new string joined_string preprocessed from the input string is proportional to the length of the input string. + +class Solution: + def isPalindrome(self, s: str) -> bool: + # Pre-processing using regular expression + joined_string = re.sub(r"[^a-zA-Z]", "", s.lower()) + str_length = len(joined_string) + + + # for loop n/2, two pointer for verify same or not + for i in range(str_length // 2): + end = str_length - i - 1 + if not self.check_palindrome(i, end, joined_string): + return False + + return True + + def check_palindrome(self, i, end, joined_string) -> bool: + left = joined_string[i] + right = joined_string[end] + + if left == right: + return True + else: + return False + +if __name__ == "__main__": + solution = Solution() + + # test case + test_string = [ + "A man, a plan, a canal: Panama", # True (회문) + "race a car", # False (회문 아님) + " ", # True (빈 문자열도 회문으로 간주) + "abba", # True (회문) + "abcba", # True (회문) + ] + + for index, test in enumerate(test_string): + print(f"start {index} test") + print(f"input : {test}") + print(f"Is valid palindrome ? {solution.isPalindrome(test)}\n") + + + From fdbee2cd4851aed6e226b817017de5b89b5de146 Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 14 Dec 2024 14:53:22 +0900 Subject: [PATCH 3/5] add solution top k frequent elements --- top-k-frequent-elements/JonghunLee.py | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 top-k-frequent-elements/JonghunLee.py diff --git a/top-k-frequent-elements/JonghunLee.py b/top-k-frequent-elements/JonghunLee.py new file mode 100644 index 000000000..4dc1f9705 --- /dev/null +++ b/top-k-frequent-elements/JonghunLee.py @@ -0,0 +1,41 @@ +from typing import List + +# Time Complexity O(n log n) +# - traversing for loop takes O(n) to create hash dictionary, +# - and when sorting by sorted function(TimSort) it takes O(nlogn) +# Space Complexity O(n log n) +# - creating hash dictionary takes O(n) +# - and when sorting takes O(n), hash[x] occupy O(1) + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + hash = dict() + + # for loop nums to set count for each element in hash(dictionary) + for num in nums: + if num in hash: + hash[num] += 1 + else: + hash[num] = 1 + + # sort (TimSort), using lambda function to set a sorting key which is a count + return sorted(hash, key=lambda x: hash[x], reverse=True)[:k] + +if __name__ == "__main__": + solution = Solution() + + # test case + nums_list = [ + [1,1,1,2,2,3], # [1, 2] + [1] # [1] + ] + k_list = [2, 1] + + for i in range(2): + nums = nums_list[i] + k = k_list[i] + result = solution.topKFrequent(nums, k) + print(f"start{i}") + print(f"input : {nums}, {k}") + print(f"result : {result}") + From 081da4e824815c9248773294e6d0e0fdec1aa535 Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 14 Dec 2024 14:56:29 +0900 Subject: [PATCH 4/5] add CRLF --- top-k-frequent-elements/JonghunLee.py | 1 - valid-palindrome/JonghunLee.py | 3 --- 2 files changed, 4 deletions(-) diff --git a/top-k-frequent-elements/JonghunLee.py b/top-k-frequent-elements/JonghunLee.py index 4dc1f9705..d2b585fed 100644 --- a/top-k-frequent-elements/JonghunLee.py +++ b/top-k-frequent-elements/JonghunLee.py @@ -38,4 +38,3 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: print(f"start{i}") print(f"input : {nums}, {k}") print(f"result : {result}") - diff --git a/valid-palindrome/JonghunLee.py b/valid-palindrome/JonghunLee.py index 5a3cd64a8..65d011b7b 100644 --- a/valid-palindrome/JonghunLee.py +++ b/valid-palindrome/JonghunLee.py @@ -46,6 +46,3 @@ def check_palindrome(self, i, end, joined_string) -> bool: print(f"start {index} test") print(f"input : {test}") print(f"Is valid palindrome ? {solution.isPalindrome(test)}\n") - - - From a085b494ac86db8253c0402b2c085ca77413e83f Mon Sep 17 00:00:00 2001 From: jonghun Date: Sat, 14 Dec 2024 15:09:22 +0900 Subject: [PATCH 5/5] add filename --- contains-duplicate/{JonghunLee.py => rivkode.py} | 0 top-k-frequent-elements/{JonghunLee.py => rivkode.py} | 0 valid-palindrome/{JonghunLee.py => rivkode.py} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{JonghunLee.py => rivkode.py} (100%) rename top-k-frequent-elements/{JonghunLee.py => rivkode.py} (100%) rename valid-palindrome/{JonghunLee.py => rivkode.py} (100%) diff --git a/contains-duplicate/JonghunLee.py b/contains-duplicate/rivkode.py similarity index 100% rename from contains-duplicate/JonghunLee.py rename to contains-duplicate/rivkode.py diff --git a/top-k-frequent-elements/JonghunLee.py b/top-k-frequent-elements/rivkode.py similarity index 100% rename from top-k-frequent-elements/JonghunLee.py rename to top-k-frequent-elements/rivkode.py diff --git a/valid-palindrome/JonghunLee.py b/valid-palindrome/rivkode.py similarity index 100% rename from valid-palindrome/JonghunLee.py rename to valid-palindrome/rivkode.py