From 8cc191ce7124fee0837b2b1961bf5c9bbe219607 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 9 Dec 2024 11:45:12 +0900 Subject: [PATCH 01/14] Add placeholder file for 'Contains Duplicate' problem --- contains-duplicate/KwonNayeon.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 contains-duplicate/KwonNayeon.py diff --git a/contains-duplicate/KwonNayeon.py b/contains-duplicate/KwonNayeon.py new file mode 100644 index 000000000..e69de29bb From abc84fcaabce38d6bc398ac7e60b7ef58a3d9367 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 10 Dec 2024 12:31:33 +0900 Subject: [PATCH 02/14] Add solution for "217. Contains Duplicate" --- contains-duplicate/KwonNayeon.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/contains-duplicate/KwonNayeon.py b/contains-duplicate/KwonNayeon.py index e69de29bb..afe1cfc43 100644 --- a/contains-duplicate/KwonNayeon.py +++ b/contains-duplicate/KwonNayeon.py @@ -0,0 +1,39 @@ +""" +Title: 217. Contains Duplicate +Link: https://leetcode.com/problems/contains-duplicate/ + +Summary: + - 주어진 배열 `nums`에서 어떤 값이 한 번 이상 등장하면 True를 반환하고, 배열의 모든 값이 유일한 경우에는 False를 반환함 + - Input: `nums = [1,2,3,1]` + - Output: `True` + +Conditions: + - 중복이 있으면: 배열에서 적어도 하나의 값이 두 번 이상 등장하면 `True` 반환 + - 중복이 없으면: 배열의 모든 값이 유일하면 `False` 반환 +""" + +""" +First Try +Time Complexity: + - O(n) * O(n) = O(n^2): `for` 루프에서 `nums.count(i)`를 호출할 때마다 리스트를 순회하므로, 전체 시간 복잡도는 `O(n^2)` +""" +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + for i in nums: + if nums.count(i) > 1: + return True + return False + +""" +Second Try (set를 활용하여 이미 본 요소를 효율적으로 추적하는 방법) +Time Complexity: + - O(n): `for` 루프에서 각 숫자에 대해 `in` 연산과 `add` 연산이 상수 시간 O(1)으로 처리되므로, 전체 시간 복잡도는 O(n) +""" +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + seen = set() + for i in nums: + if i in seen: + return True + seen.add(i) + return False \ No newline at end of file From c8199c99b9277c45372833da4a4d4b425eca6464 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 10 Dec 2024 12:52:56 +0900 Subject: [PATCH 03/14] Add newline at the end of the file to comply with the CI requirements --- contains-duplicate/KwonNayeon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/KwonNayeon.py b/contains-duplicate/KwonNayeon.py index afe1cfc43..090989dab 100644 --- a/contains-duplicate/KwonNayeon.py +++ b/contains-duplicate/KwonNayeon.py @@ -36,4 +36,4 @@ def containsDuplicate(self, nums: List[int]) -> bool: if i in seen: return True seen.add(i) - return False \ No newline at end of file + return False From 63441749896dc5b4df10217bcd01acc72b55cb90 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 10 Dec 2024 17:53:02 +0900 Subject: [PATCH 04/14] Add solution for Valid Palindrome problem --- valid-palindrome/KwonNayeon.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 valid-palindrome/KwonNayeon.py diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py new file mode 100644 index 000000000..a05b33c05 --- /dev/null +++ b/valid-palindrome/KwonNayeon.py @@ -0,0 +1,24 @@ +""" +Title: 215. Valid Palindrome +Link: https://leetcode.com/problems/valid-palindrome/ + +Summary: + - Palindrome이라면 True, 아니라면 False를 반환하는 문제. + - Palindrome이란, 대문자를 소문자로 변환하고 알파벳과 숫자 이외의 문자를 제거한 후에도 + 앞으로 읽어도 뒤에서부터 읽어도 동일한 단어를 뜻함. + - e.g. racecar + +Conditions: + - 입력 문자열이 Palindrome인 경우: `True` 반환 + - Palindrome이 아닌 경우: `False` 반환 + +Time Complexity: + - O(n) +""" + +class Solution: + def isPalindrome(self, s: str) -> bool: + s = re.sub(r'[^a-zA-z]', '', s).lower() + if s == s[::-1]: + return True + return False From 365c1cc807eb019cc3c0348244596231d8d7b357 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 10 Dec 2024 18:02:56 +0900 Subject: [PATCH 05/14] Update description and conditions for Valid Palindrome problem --- valid-palindrome/KwonNayeon.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index a05b33c05..71509b4ce 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -1,11 +1,11 @@ """ -Title: 215. Valid Palindrome +Title: 215. Valid Palindrome Link: https://leetcode.com/problems/valid-palindrome/ Summary: - Palindrome이라면 True, 아니라면 False를 반환하는 문제. - - Palindrome이란, 대문자를 소문자로 변환하고 알파벳과 숫자 이외의 문자를 제거한 후에도 - 앞으로 읽어도 뒤에서부터 읽어도 동일한 단어를 뜻함. + - Palindrome이란, 앞으로 읽어도 뒤에서부터 읽어도 동일한 단어를 뜻함. + - 추가 조건: 대소문자를 구분하지 않으며, 알파벳과 숫자 이외의 문자는 제거해야 함. - e.g. racecar Conditions: @@ -15,7 +15,6 @@ Time Complexity: - O(n) """ - class Solution: def isPalindrome(self, s: str) -> bool: s = re.sub(r'[^a-zA-z]', '', s).lower() From 52125486138aa0114b94f3508af207e1863a2582 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Wed, 11 Dec 2024 12:55:33 +0900 Subject: [PATCH 06/14] Solved 237. Top K Frequent Elements problem --- top-k-frequent-elements/KwonNayeon.py | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 top-k-frequent-elements/KwonNayeon.py diff --git a/top-k-frequent-elements/KwonNayeon.py b/top-k-frequent-elements/KwonNayeon.py new file mode 100644 index 000000000..3dc9d1fe6 --- /dev/null +++ b/top-k-frequent-elements/KwonNayeon.py @@ -0,0 +1,33 @@ +""" +Title: 237. Top K Frequent Elements +Link: https://leetcode.com/problems/top-k-frequent-elements/ + +Question: + - Given an integer array `nums` and an integer `k`, return the `k` most frequent elements. + - You may return the answer in any order. + +Constraints: + - 1 <= nums.length <= 10^5 + - -10^4 <= nums[i] <= 10^4 + - k is in the range [1, the number of unique elements in the array]. + - The answer is guaranteed to be unique. + +Time Complexity: + - O(n log n) +Space Complexity: + - O(n) +""" + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + frequency = {} + result = [] + for num in nums: + if num in frequency: + frequency[num] += 1 + else: + frequency[num] = 1 + sorted_frequency = sorted(frequency.items(), key=lambda x: x[1], reverse=True) + for i in range(k): + result.append(sorted_frequency[i][0]) + return result \ No newline at end of file From 228f63b1876186351752234b811bb13cfc337183 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Wed, 11 Dec 2024 12:56:40 +0900 Subject: [PATCH 07/14] Add newline at the end of the file to comply with the CI requirements --- top-k-frequent-elements/KwonNayeon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/top-k-frequent-elements/KwonNayeon.py b/top-k-frequent-elements/KwonNayeon.py index 3dc9d1fe6..401538132 100644 --- a/top-k-frequent-elements/KwonNayeon.py +++ b/top-k-frequent-elements/KwonNayeon.py @@ -30,4 +30,5 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: sorted_frequency = sorted(frequency.items(), key=lambda x: x[1], reverse=True) for i in range(k): result.append(sorted_frequency[i][0]) - return result \ No newline at end of file + return result + \ No newline at end of file From a4fa1ea0e9387258e4c786e37d6415f9bd42650f Mon Sep 17 00:00:00 2001 From: Nayeon Date: Wed, 11 Dec 2024 12:59:13 +0900 Subject: [PATCH 08/14] Fix: remove unnecessary spaces before newline to comply with CI requirements --- top-k-frequent-elements/KwonNayeon.py | 1 - 1 file changed, 1 deletion(-) diff --git a/top-k-frequent-elements/KwonNayeon.py b/top-k-frequent-elements/KwonNayeon.py index 401538132..cdfab0b31 100644 --- a/top-k-frequent-elements/KwonNayeon.py +++ b/top-k-frequent-elements/KwonNayeon.py @@ -31,4 +31,3 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: for i in range(k): result.append(sorted_frequency[i][0]) return result - \ No newline at end of file From f7ae499159a10c2fb5e55295b6ab0d6a379f5d27 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Thu, 12 Dec 2024 14:02:11 +0900 Subject: [PATCH 09/14] Solved 128. Longest Consecutive Sequence problem --- longest-consecutive-sequence/KwonNayeon.py | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-consecutive-sequence/KwonNayeon.py diff --git a/longest-consecutive-sequence/KwonNayeon.py b/longest-consecutive-sequence/KwonNayeon.py new file mode 100644 index 000000000..ea15875f8 --- /dev/null +++ b/longest-consecutive-sequence/KwonNayeon.py @@ -0,0 +1,38 @@ +""" +Title: 128. Longest Consecutive Sequence +Link: https://leetcode.com/problems/longest-consecutive-sequence/ + +Question: + - Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. + - You must write an algorithm that runs in O(n) time. + +Constraints: + - 0 <= nums.length <= 10^5 + - -10^9 <= nums[i] <= 10^9 + +Time Complexity: + - O(n log n) +Space Complexity: + - O(n) + +Notes: + - sorted(nums)를 사용하면 TC가 O(n log n)이 되어 문제의 조건을 충족하지 못하지만, 이 방법이 제가 생각해서 풀 수 있는 최선이라 일단 이대로 제출합니다! 다른 분들 답안 참고하여 다시 풀어보겠습니다 :) +""" + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums = sorted(nums) + max_length = 0 + current_length = 1 + + for i in range(1, len(nums)): + if nums[i] == nums[i-1] + 1: + current_length += 1 + elif nums[i] == nums[i-1]: + continue + else: + max_length = max(max_length, current_length) + current_length = 1 + + max_length = max(max_length, current_length) + return max_length From 65c2c59f447f3aea0d9889046e161672b57afcef Mon Sep 17 00:00:00 2001 From: Nayeon Date: Thu, 12 Dec 2024 15:29:35 +0900 Subject: [PATCH 10/14] Solved 198. House Robber using Python code --- house-robber/KwonNayeon.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 house-robber/KwonNayeon.py diff --git a/house-robber/KwonNayeon.py b/house-robber/KwonNayeon.py new file mode 100644 index 000000000..600128dcb --- /dev/null +++ b/house-robber/KwonNayeon.py @@ -0,0 +1,28 @@ +""" +Title: 198. House Robber + +Constraints: + - 1 <= nums.length <= 100 + - 0 <= nums[i] <= 400 + +Time Complexity: + - O(n) +Space Complexity: + - O(n) +""" + +class Solution: + def rob(self, nums: List[int]) -> int: + + if len(nums) == 1: + return nums[0] + elif len(nums) == 2: + return max(nums[0], nums[1]) + + dp = [0]*len(nums) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, len(nums)): + dp[i] = max(dp[i-1], nums[i] + dp[i-2]) + return dp[-1] From dd6ca48be4da2b1398b0b79b5c9363355acc67eb Mon Sep 17 00:00:00 2001 From: Nayeon Date: Fri, 13 Dec 2024 16:06:21 +0900 Subject: [PATCH 11/14] refactor: Improve time complexity using heap for topKFrequent --- top-k-frequent-elements/KwonNayeon.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/top-k-frequent-elements/KwonNayeon.py b/top-k-frequent-elements/KwonNayeon.py index cdfab0b31..4c11c5c9b 100644 --- a/top-k-frequent-elements/KwonNayeon.py +++ b/top-k-frequent-elements/KwonNayeon.py @@ -18,16 +18,43 @@ - O(n) """ +# Original Solution class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: frequency = {} result = [] + for num in nums: if num in frequency: frequency[num] += 1 else: frequency[num] = 1 + sorted_frequency = sorted(frequency.items(), key=lambda x: x[1], reverse=True) + for i in range(k): result.append(sorted_frequency[i][0]) + + return result + +# Improved Solution using Heap +# Time Complexity: O(n log k) +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + frequency = {} + result = [] + heap = [] + + for num in nums: + if num in frequency: + frequency[num] += 1 + else: + frequency[num] = 1 + + for num, freq in frequency.items(): + heapq.heappush(heap, (-freq, num)) + + for i in range(k): + result.append(heapq.heappop(heap)[1]) + return result From dfb6f0c50aa55547bf831d9a6ebb4ba74c9b1895 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Fri, 13 Dec 2024 18:58:06 +0900 Subject: [PATCH 12/14] Fix: Include numeric characters in palindrome check --- valid-palindrome/KwonNayeon.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index 71509b4ce..b543fc371 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -14,10 +14,10 @@ Time Complexity: - O(n) +Space Complexity: + - O(n) """ class Solution: def isPalindrome(self, s: str) -> bool: - s = re.sub(r'[^a-zA-z]', '', s).lower() - if s == s[::-1]: - return True - return False + s = re.sub(r'[^a-zA-z0-9]', '', s).lower() + if s == s[::-1] From a211d72e4695cfa2ab054490908a2dbfdabd308c Mon Sep 17 00:00:00 2001 From: Nayeon Date: Fri, 13 Dec 2024 19:02:20 +0900 Subject: [PATCH 13/14] Fix: Updated isPalindrome function to return true/false. --- valid-palindrome/KwonNayeon.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index b543fc371..5bb039da8 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -20,4 +20,7 @@ class Solution: def isPalindrome(self, s: str) -> bool: s = re.sub(r'[^a-zA-z0-9]', '', s).lower() - if s == s[::-1] + if s == s[::-1]: + return True + return False + From 2173819b2bd95ceede3c1b8e8c4b5164485fe5a3 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Sun, 15 Dec 2024 08:01:02 +0900 Subject: [PATCH 14/14] Add placeholder file for 'Valid Anagram' problem --- valid-anagram/KwonNayeon.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 valid-anagram/KwonNayeon.py diff --git a/valid-anagram/KwonNayeon.py b/valid-anagram/KwonNayeon.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/valid-anagram/KwonNayeon.py @@ -0,0 +1 @@ +