From 270473dc2411f1c2219a61ca727a66ca4b5f578b Mon Sep 17 00:00:00 2001 From: pmjuu Date: Wed, 4 Dec 2024 14:18:54 -0500 Subject: [PATCH 1/6] feat: solve contains duplicate --- contains-duplicate/pmjuu.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 contains-duplicate/pmjuu.py diff --git a/contains-duplicate/pmjuu.py b/contains-duplicate/pmjuu.py new file mode 100644 index 000000000..0648d87c5 --- /dev/null +++ b/contains-duplicate/pmjuu.py @@ -0,0 +1,3 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(nums) != len(set(nums)) From 9b2784c9874825110d23aaf46b8fabe7cc4f2a1a Mon Sep 17 00:00:00 2001 From: pmjuu Date: Thu, 5 Dec 2024 22:55:17 -0500 Subject: [PATCH 2/6] feat: solve valid palindrome --- valid-palindrome/pmjuu.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 valid-palindrome/pmjuu.py diff --git a/valid-palindrome/pmjuu.py b/valid-palindrome/pmjuu.py new file mode 100644 index 000000000..83ac4a78e --- /dev/null +++ b/valid-palindrome/pmjuu.py @@ -0,0 +1,22 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + # two pointer + left, right = 0, len(s) - 1 + + while left < right: + # compare only alphanumeric characters + while left < right and not s[left].isalnum(): + left += 1 + while left < right and not s[right].isalnum(): + right -= 1 + + # compare with lowercase + if s[left].lower() != s[right].lower(): + return False + + # move pointers + left += 1 + right -= 1 + + return True + \ No newline at end of file From 69a28ae82dd3da63d8fc79f784438661d43be44e Mon Sep 17 00:00:00 2001 From: pmjuu Date: Thu, 5 Dec 2024 23:00:01 -0500 Subject: [PATCH 3/6] fix: modify line break --- valid-palindrome/pmjuu.py | 1 - 1 file changed, 1 deletion(-) diff --git a/valid-palindrome/pmjuu.py b/valid-palindrome/pmjuu.py index 83ac4a78e..004531ba7 100644 --- a/valid-palindrome/pmjuu.py +++ b/valid-palindrome/pmjuu.py @@ -19,4 +19,3 @@ def isPalindrome(self, s: str) -> bool: right -= 1 return True - \ No newline at end of file From 4a27dad9f3972fe6756e3e08c24b77b2b62c02a5 Mon Sep 17 00:00:00 2001 From: pmjuu Date: Mon, 9 Dec 2024 13:55:08 -0500 Subject: [PATCH 4/6] feat: solve top k frequent elements --- top-k-frequent-elements/pmjuu.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 top-k-frequent-elements/pmjuu.py diff --git a/top-k-frequent-elements/pmjuu.py b/top-k-frequent-elements/pmjuu.py new file mode 100644 index 000000000..c1840cccb --- /dev/null +++ b/top-k-frequent-elements/pmjuu.py @@ -0,0 +1,24 @@ +from collections import Counter +from typing import List + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + # 빈도 계산 + count = Counter(nums) + n = len(nums) + + # 빈도수를 기준으로 버킷 생성 (0에서 n까지) + buckets = [[] for _ in range(n + 1)] + + # 각 숫자를 해당 빈도수의 버킷에 추가 + for num, freq in count.items(): + buckets[freq].append(num) + + # 빈도가 높은 순서대로 k개의 숫자를 추출 + result = [] + for freq in range(n, 0, -1): + if buckets[freq]: + result.extend(buckets[freq]) + + if len(result) == k: + return result From b4cb47c79a7ef44ede83276bc5590a57caa62733 Mon Sep 17 00:00:00 2001 From: pmjuu Date: Tue, 10 Dec 2024 15:18:35 -0500 Subject: [PATCH 5/6] feat: solve longest-consecutive-sequence --- longest-consecutive-sequence/pmjuu.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 longest-consecutive-sequence/pmjuu.py diff --git a/longest-consecutive-sequence/pmjuu.py b/longest-consecutive-sequence/pmjuu.py new file mode 100644 index 000000000..fe350077c --- /dev/null +++ b/longest-consecutive-sequence/pmjuu.py @@ -0,0 +1,22 @@ +from typing import List + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + # Convert to set for O(1) lookups + num_set = set(nums) + longest_length = 0 + + for num in num_set: + # Only start counting if num is the start of a sequence + if num - 1 not in num_set: + current_num = num + current_length = 1 + + # Count the length of the sequence + while current_num + 1 in num_set: + current_num += 1 + current_length += 1 + + longest_length = max(longest_length, current_length) + + return longest_length From de3351dabd354aa6e32cb369cde2b2cc71ce3107 Mon Sep 17 00:00:00 2001 From: pmjuu Date: Wed, 11 Dec 2024 21:21:04 -0500 Subject: [PATCH 6/6] feat: solve house-robber --- house-robber/pmjuu.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 house-robber/pmjuu.py diff --git a/house-robber/pmjuu.py b/house-robber/pmjuu.py new file mode 100644 index 000000000..0f8df0ac7 --- /dev/null +++ b/house-robber/pmjuu.py @@ -0,0 +1,16 @@ +from typing import List + +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + # prev1: 이전 집까지의 최대 이익 + # prev2: 전전 집까지의 최대 이익 + prev1, prev2 = 0, 0 + for num in nums: + temp = prev1 + prev1 = max(prev2 + num, prev1) # 현재 집을 털었을 때와 안 털었을 때 중 더 큰 이익 선택 + prev2 = temp + + return prev1