diff --git a/3sum/freemjstudio.py b/3sum/freemjstudio.py index 92a6a3f66f..b6d16520c8 100644 --- a/3sum/freemjstudio.py +++ b/3sum/freemjstudio.py @@ -1,16 +1,16 @@ class Solution: def threeSum(self, nums: list[int]) -> list[list[int]]: answer = [] - nums.sort() - + nums.sort() + for i in range(len(nums)): # 중복된 원소가 있는 경우 똑같이 검사할 필요가 없으므로 건너뛴다. if i > 0 and nums[i] == nums[i-1]: - continue + continue left, right = i+1, len(nums) - 1 - while left < right: + while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: answer.append([nums[i], nums[left], nums[right]]) @@ -18,12 +18,12 @@ def threeSum(self, nums: list[int]) -> list[list[int]]: left += 1 right -= 1 - # 중복된 원소가 있는 경우 같은 정답 조합을 방지한다 + # 중복된 원소가 있는 경우 같은 정답 조합을 방지한다 while left < right and nums[left] == nums[left -1]: left += 1 elif total < 0: left += 1 - else: + else: right -= 1 return answer diff --git a/climbing-stairs/freemjstudio.py b/climbing-stairs/freemjstudio.py index 4b2e390a90..29bc2fa9a3 100644 --- a/climbing-stairs/freemjstudio.py +++ b/climbing-stairs/freemjstudio.py @@ -1,12 +1,12 @@ class Solution: def climbStairs(self, n: int) -> int: - answer = 0 + answer = 0 if n == 1: # ways(1) = 1 return 1 if n == 2: # ways(2) = 2 return 2 - + dp = [0] * 46 # 1 <= n <= 45 dp[1] = 1 dp[2] = 2 diff --git a/combination-sum/freemjstudio.py b/combination-sum/freemjstudio.py new file mode 100644 index 0000000000..d6824f6949 --- /dev/null +++ b/combination-sum/freemjstudio.py @@ -0,0 +1,23 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + answer = [] + + def backtracking(start_idx, total, path): + if target == total: + answer.append(path[:]) # 새로운 리스트 객체를 만들어서 저장함. + # 리스트가 mutable 객체이므로 path[:] 로 복사하지 않으면 path 가 바뀔 때 answer 안의 값도 바뀌게 된다. + return + + if target < total: + return + + for idx in range(start_idx, len(candidates)): + new_node = candidates[idx] # 동일한 숫자 재사용 가능함. start_idx 도 포함한다. + path.append(new_node) + + backtracking(idx, total + new_node, path) + # 선택 취소 + path.pop() + + backtracking(0, 0, []) + return answer diff --git a/contains-duplicate/freemjstudio.py b/contains-duplicate/freemjstudio.py index 6048a3c5ea..b9cdeddb4c 100644 --- a/contains-duplicate/freemjstudio.py +++ b/contains-duplicate/freemjstudio.py @@ -1,6 +1,6 @@ class Solution: def containsDuplicate(self, nums: List[int]) -> bool: - first = len(nums) # O(1) : 리스트 객체는 이미 자기자신의 길이를 저장하고 있음 + first = len(nums) # O(1) : 리스트 객체는 이미 자기자신의 길이를 저장하고 있음 set_nums = set(nums) # O(N) second = len(set_nums) return (first != second) diff --git a/number-of-1-bits/freemjstudio.py b/number-of-1-bits/freemjstudio.py new file mode 100644 index 0000000000..ef95ab1dea --- /dev/null +++ b/number-of-1-bits/freemjstudio.py @@ -0,0 +1,15 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + binary = bin(n) + return binary.count("1") + +# bin() 을 못쓰는 경우의 풀이 +class Solution: + def hammingWeight(self, n: int) -> int: + count = 0 + + while n > 0: + count += n % 2 + n //=2 + + return count diff --git a/product-of-array-except-self/freemjstudio.py b/product-of-array-except-self/freemjstudio.py new file mode 100644 index 0000000000..09df575b56 --- /dev/null +++ b/product-of-array-except-self/freemjstudio.py @@ -0,0 +1,16 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [1] * len(nums) + + # left -> right + for i in range(1, len(nums)): + answer[i] = answer[i-1] * nums[i-1] # 왼쪽에 있는 것들을 곱하자 + + # right -> left + right_product = 1 + for i in range(len(nums)-1, -1, -1): + answer[i] = answer[i] * right_product + right_product = right_product * nums[i] + + + return answer diff --git a/top-k-frequent-elements/freemjstudio.py b/top-k-frequent-elements/freemjstudio.py index 184c87aeee..ca5228336a 100644 --- a/top-k-frequent-elements/freemjstudio.py +++ b/top-k-frequent-elements/freemjstudio.py @@ -5,18 +5,18 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: answer = [] count = dict() heap = [] - if len(nums) <= 1: + if len(nums) <= 1: return nums - # O(N) - for num in nums: + # O(N) + for num in nums: count[num] = count.get(num, 0) + 1 - - # heap 의 크기는 최대 k 로 제한함. + + # heap 의 크기는 최대 k 로 제한함. # unique 한 M개의 숫자를 선형 순회 O(M) for num, freq in count.items(): - if len(heap) < k: + if len(heap) < k: heapq.heappush(heap, (freq, num)) # O(logK) - else: + else: if heap[0][0] < freq: heapq.heappop(heap) # O(logK) heapq.heappush(heap, (freq, num)) # O(logK) @@ -25,5 +25,4 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: k, v = heapq.heappop(heap) answer.append(v) - return answer - + return answer diff --git a/two-sum/freemjstudio.py b/two-sum/freemjstudio.py index a274028f5c..69426fb065 100644 --- a/two-sum/freemjstudio.py +++ b/two-sum/freemjstudio.py @@ -5,8 +5,8 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): current_num = nums[i] diff = target - current_num - if diff in hashmap.keys(): - return [i, hashmap[diff]] + if diff in hashmap.keys(): + return [i, hashmap[diff]] hashmap[current_num] = i # store the index of current num - + return answer diff --git a/valid-anagram/freemjstudio.py b/valid-anagram/freemjstudio.py index 44fa9a12ea..43da0864a5 100644 --- a/valid-anagram/freemjstudio.py +++ b/valid-anagram/freemjstudio.py @@ -1,5 +1,5 @@ """ -첫번째 풀이 +첫번째 풀이 시간 복잡도 : O(n log n) """ @@ -14,19 +14,19 @@ def isAnagram(self, s: str, t: str) -> bool: class Solution: def isAnagram(self, s: str, t: str) -> bool: - # 반례 : s = ab, t = a + # 반례 : s = ab, t = a # 이 경우에는 t에 없는 b가 존재하므로 False를 반환해야 한다. if len(s) != len(t): - return False + return False counter = {} - for ch in s: + for ch in s: counter[ch] = counter.get(ch, 0) + 1 - - for ch in t: + + for ch in t: if ch not in counter: - return False + return False counter[ch] -= 1 if counter[ch] < 0: - return False + return False return True diff --git a/valid-palindrome/freemjstudio.py b/valid-palindrome/freemjstudio.py new file mode 100644 index 0000000000..cb93b4e4e9 --- /dev/null +++ b/valid-palindrome/freemjstudio.py @@ -0,0 +1,21 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + characters = "abcdefghijklmnopqrstuvwxyz0123456789" + s = s.lower() + + filtered = "" + for ch in s: + if ch in characters: + filtered += ch + + n = len(filtered) + left = 0 + right = n-1 + while left <= n//2 and right >= n//2: + if filtered[left] == filtered[right]: + left += 1 + right -= 1 + else: + return False + + return True