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)) 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 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 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 diff --git a/valid-palindrome/pmjuu.py b/valid-palindrome/pmjuu.py new file mode 100644 index 000000000..004531ba7 --- /dev/null +++ b/valid-palindrome/pmjuu.py @@ -0,0 +1,21 @@ +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