From 7555cad78527c692f8d5dd4ef576ce647598638a Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 9 Dec 2024 21:32:02 +0900 Subject: [PATCH 1/5] Contains Duplicate --- contains-duplicate/thispath98.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 contains-duplicate/thispath98.py diff --git a/contains-duplicate/thispath98.py b/contains-duplicate/thispath98.py new file mode 100644 index 000000000..21c471339 --- /dev/null +++ b/contains-duplicate/thispath98.py @@ -0,0 +1,15 @@ +""" +# Time Complexity: O(N) +- N번 순회 +# Space Compelexity: O(N) +- 최악의 경우 (중복된 값이 없을 경우) N개 저장 +""" +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + num_dict = {} + for num in nums: + if num not in num_dict: + num_dict[num] = True + else: + return True + return False From 8918c97b1fd1c593830e40ae82dbeb86e6a88644 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 9 Dec 2024 21:50:53 +0900 Subject: [PATCH 2/5] Valid Palindrome --- valid-palindrome/thispath98.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 valid-palindrome/thispath98.py diff --git a/valid-palindrome/thispath98.py b/valid-palindrome/thispath98.py new file mode 100644 index 000000000..142bc1a13 --- /dev/null +++ b/valid-palindrome/thispath98.py @@ -0,0 +1,13 @@ +""" +# Time Complexity: O(N) +- N개의 char를 각각 한번씩 순회 +# Space Compelexity: O(N) +- 최악의 경우 (공백이 없을 경우) N개의 char 저장 +""" +class Solution: + def isPalindrome(self, s: str) -> bool: + string = [char.lower() for char in s if char.isalnum()] + for i in range(len(string) // 2): + if string[i] != string[-i - 1]: + return False + return True From e07436ca93e5c1d6867165465c10a5f87f14cf26 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 9 Dec 2024 22:08:25 +0900 Subject: [PATCH 3/5] Top K Frequent Elements --- top-k-frequent-elements/thispath98.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 top-k-frequent-elements/thispath98.py diff --git a/top-k-frequent-elements/thispath98.py b/top-k-frequent-elements/thispath98.py new file mode 100644 index 000000000..3c21195a0 --- /dev/null +++ b/top-k-frequent-elements/thispath98.py @@ -0,0 +1,16 @@ +""" +# Time Complexity: O(N log N) +- Counter 생성: N번 순회 +- most common 연산: N log N +# Space Compelexity: O(N) +- 최악의 경우 (중복된 값이 없을 경우) N개 저장 +""" +from collections import Counter + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + count_dict = Counter(nums) + top_k_list = count_dict.most_common(k) + answer = [key for key, value in top_k_list] + return answer From 3c5e8289b24d2143612ac221588ea0b3e7af6ea6 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 9 Dec 2024 22:41:02 +0900 Subject: [PATCH 4/5] Longest Consecutive Sequence --- longest-common-subsequence/thispath98.py | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 longest-common-subsequence/thispath98.py diff --git a/longest-common-subsequence/thispath98.py b/longest-common-subsequence/thispath98.py new file mode 100644 index 000000000..5a9a1e457 --- /dev/null +++ b/longest-common-subsequence/thispath98.py @@ -0,0 +1,37 @@ +""" +# Time Complexity: O(N) +- lce_dict 생성: N +- N개의 key에 대하여 순회하면서 값 확인: N +# Space Compelexity: O(k) +- 중복되지 않은 key k개 저장 +""" +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if not nums: + return 0 + + lce_dict = {} + for num in nums: + lce_dict[num] = True + + answer = 0 + for num in nums: + cur_lce = 1 + if lce_dict.pop(num, None) is None: + continue + + down_num = num - 1 + while down_num in lce_dict: + cur_lce += 1 + lce_dict.pop(down_num) + down_num -= 1 + + up_num = num + 1 + while up_num in lce_dict: + cur_lce += 1 + lce_dict.pop(up_num) + up_num += 1 + + answer = answer if answer > cur_lce else cur_lce + + return answer From 2fd0eef5d7749a07641ef3252aae0c11fcf39940 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 9 Dec 2024 23:00:27 +0900 Subject: [PATCH 5/5] House Robber --- house-robber/thispath98.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 house-robber/thispath98.py diff --git a/house-robber/thispath98.py b/house-robber/thispath98.py new file mode 100644 index 000000000..befc6f2ba --- /dev/null +++ b/house-robber/thispath98.py @@ -0,0 +1,19 @@ +""" +# Time Complexity: O(N) +- N개의 개수를 가지는 dp 리스트를 만들고, 이를 순회 +# Space Compelexity: O(N) +- N개의 dp 리스트 저장 +""" +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + dp = [0 for _ in range(len(nums))] + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(len(nums) - 2): + dp[i + 2] = max(dp[i] + nums[i + 2], dp[i + 1]) + + return max(dp[-2], dp[-1])