diff --git a/combination-sum/hi-rachel.py b/combination-sum/hi-rachel.py new file mode 100644 index 000000000..abad32ddc --- /dev/null +++ b/combination-sum/hi-rachel.py @@ -0,0 +1,20 @@ +# DFS + 백트래킹 +# 중복 조합 문제 +# O(2^t) time, O(재귀 깊이 (t) + 결과 조합의 수 (len(result))) space + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + nums, result = [], [] + def dfs(start, total): + if total > target: + return + if total == target: + result.append(nums[:]) + for i in range(start, len(candidates)): + num = candidates[i] + nums.append(num) + dfs(i, total + num) + nums.pop() + + dfs(0, 0) + return result diff --git a/decode-ways/hi-rachel.py b/decode-ways/hi-rachel.py new file mode 100644 index 000000000..a1d684eb1 --- /dev/null +++ b/decode-ways/hi-rachel.py @@ -0,0 +1,19 @@ +# 디코드 가능 1 ~ 26 +# 숫자의 첫 번째 자리가 0이라면 decode x +# O(n) time, O(n) space + +class Solution: + def numDecodings(self, s: str) -> int: + memo = {len(s): 1} # 문자열 끝에 도달했을 때는 경우의 수 1 + + def dfs(start): + if start in memo: # 이미 계산한 위치 재계산 x + return memo[start] + if s[start] == "0": + memo[start] = 0 + elif start + 1 < len(s) and int(s[start:start + 2]) < 27: # 두 자리로 해석 가능할 때 + memo[start] = dfs(start + 1) + dfs(start + 2) # 첫 한 자리만 decode 경우 + 두 자리 한꺼번에 decode 경우 + else: + memo[start] = dfs(start + 1) # 두 자리로 decode 불가능할 때 -> 한 자리만 decode + return memo[start] + return dfs(0) diff --git a/maximum-subarray/hi-rachel.py b/maximum-subarray/hi-rachel.py new file mode 100644 index 000000000..b762e582f --- /dev/null +++ b/maximum-subarray/hi-rachel.py @@ -0,0 +1,35 @@ +# 최대 부분 배열 합 문제 + +# O(n^2) time, O(1) space +# Time Limit Exceeded + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_total = nums[0] + for i in range(len(nums)): + total = 0 + for j in range(i, len(nums)): + total += nums[j] + max_total = max(total, max_total) + return max_total + +# 개선 풀이 +# O(n) time, O(1) space +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_total = nums[0] + total = nums[0] + for i in range(1, len(nums)): + total = max(nums[i], total + nums[i]) + max_total = max(total, max_total) + return max_total + +# DP 풀이 +# O(n) time, O(n) space +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + dp = [0] * len(nums) + dp[0] = nums[0] + for i in range(1, len(nums)): + dp[i] = max(nums[i], dp[i - 1] + nums[i]) + return max(dp) diff --git a/number-of-1-bits/hi-rachel.py b/number-of-1-bits/hi-rachel.py new file mode 100644 index 000000000..16f13b6e8 --- /dev/null +++ b/number-of-1-bits/hi-rachel.py @@ -0,0 +1,26 @@ +# O(log n) time, O(1) space +# % 나머지, // 몫 + +class Solution: + def hammingWeight(self, n: int) -> int: + cnt = 0 + + while n > 0: + if (n % 2) == 1: + cnt += 1 + n //= 2 + + return cnt + + +# O(log n) time, O(log n) space +class Solution: + def hammingWeight(self, n: int) -> int: + return bin(n).count("1") + + +# TS 풀이 +# O(log n) time, O(log n) space +# function hammingWeight(n: number): number { +# return n.toString(2).split('').filter(bit => bit === '1').length; +# }; diff --git a/valid-palindrome/hi-rachel.py b/valid-palindrome/hi-rachel.py new file mode 100644 index 000000000..c509fab9a --- /dev/null +++ b/valid-palindrome/hi-rachel.py @@ -0,0 +1,20 @@ +# O(n) time, O(1) space +# isalnum() -> 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴. + +class Solution: + def isPalindrome(self, s: str) -> bool: + l = 0 + r = len(s) - 1 + + while l < r: + if not s[l].isalnum(): + l += 1 + elif not s[r].isalnum(): + r -= 1 + elif s[l].lower() == s[r].lower(): + l += 1 + r -= 1 + else: + return False + return True + diff --git a/valid-palindrome/hi-rachel.ts b/valid-palindrome/hi-rachel.ts new file mode 100644 index 000000000..c020ef1a7 --- /dev/null +++ b/valid-palindrome/hi-rachel.ts @@ -0,0 +1,21 @@ +// O(n) time, O(1) space + +function isPalindrome(s: string): boolean { + let low = 0, + high = s.length - 1; + + while (low < high) { + while (low < high && !s[low].match(/[0-9a-zA-Z]/)) { + low++; + } + while (low < high && !s[high].match(/[0-9a-zA-Z]/)) { + high--; + } + if (s[low].toLowerCase() !== s[high].toLowerCase()) { + return false; + } + low++; + high--; + } + return true; +}