From e2184f3fcc5bf80194a93534b201fc61cbb7dfaa Mon Sep 17 00:00:00 2001 From: hestia-park Date: Fri, 18 Apr 2025 18:56:21 -0400 Subject: [PATCH] week 3 sol --- combination-sum/hestia-park.py | 18 ++++++++++++++++++ maximum-subarray/hestia-park.py | 12 ++++++++++++ number-of-1-bits/hestia-park.py | 20 ++++++++++++++++++++ valid-palindrome/hestia-park.py | 20 ++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 combination-sum/hestia-park.py create mode 100644 maximum-subarray/hestia-park.py create mode 100644 number-of-1-bits/hestia-park.py create mode 100644 valid-palindrome/hestia-park.py diff --git a/combination-sum/hestia-park.py b/combination-sum/hestia-park.py new file mode 100644 index 000000000..665916dc3 --- /dev/null +++ b/combination-sum/hestia-park.py @@ -0,0 +1,18 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + def dfs(result,remain, path, start): + if remain==0: + result.append(path[:]) + return + if remain < 0: + return + for i in range(start, len(candidates)): + path.append(candidates[i]) + dfs(result,remain - candidates[i], path, i) + path.pop() + + dfs(result,target, [], 0) + return result + + diff --git a/maximum-subarray/hestia-park.py b/maximum-subarray/hestia-park.py new file mode 100644 index 000000000..114e6def5 --- /dev/null +++ b/maximum-subarray/hestia-park.py @@ -0,0 +1,12 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + dp = 0 + max_ = float('-inf') + for i in range(len(nums)): + dp += nums[i] + max_ = max(max_, dp) + if dp < 0: + dp = 0 # 누적합이 0보다 작아지면 버리고 새로 시작 + return max_ + + diff --git a/number-of-1-bits/hestia-park.py b/number-of-1-bits/hestia-park.py new file mode 100644 index 000000000..81aecf575 --- /dev/null +++ b/number-of-1-bits/hestia-park.py @@ -0,0 +1,20 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + # ans=0 + # # bit=[] + # moc=n + # while moc > 0: + # nam=moc%2 + # if nam==1: + # ans+=1 + # moc=int((moc-nam)/2) + + # return ans + # using Brian Kernighan’s Algorithm + count = 0 + while n: + n &= n - 1 + count += 1 + return count + + diff --git a/valid-palindrome/hestia-park.py b/valid-palindrome/hestia-park.py new file mode 100644 index 000000000..4ba1e6a8f --- /dev/null +++ b/valid-palindrome/hestia-park.py @@ -0,0 +1,20 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + # lower_text = s.lower() + # clean_text = re.sub(r'[^a-z0-9]', '', lower_text) + # if len(clean_text) ==0: + # return True + + # j = len(clean_text) - 1 + # ans = True + + # for i in range(len(clean_text) // 2): + # if clean_text[i] != clean_text[j]: + # ans = False + # break + # j -= 1 + # return ans + n = "".join(c for c in s if c.isalnum()).lower() + return n == n[::-1] + +