Skip to content

[hi-rachel] WEEK 03 solutions #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions combination-sum/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions decode-ways/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions maximum-subarray/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions number-of-1-bits/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin() 함수는 처음 보았는데 흥미롭네요 😃



# TS 풀이
# O(log n) time, O(log n) space
# function hammingWeight(n: number): number {
# return n.toString(2).split('').filter(bit => bit === '1').length;
# };
20 changes: 20 additions & 0 deletions valid-palindrome/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions valid-palindrome/hi-rachel.ts
Original file line number Diff line number Diff line change
@@ -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;
}