Skip to content

Commit 4dbbb84

Browse files
authored
Merge pull request #1325 from hi-rachel/main
[hi-rachel] WEEK 03 solutions
2 parents e9a4523 + ad5aee5 commit 4dbbb84

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

combination-sum/hi-rachel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# DFS + 백트래킹
2+
# 중복 조합 문제
3+
# O(2^t) time, O(재귀 깊이 (t) + 결과 조합의 수 (len(result))) space
4+
5+
class Solution:
6+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
7+
nums, result = [], []
8+
def dfs(start, total):
9+
if total > target:
10+
return
11+
if total == target:
12+
result.append(nums[:])
13+
for i in range(start, len(candidates)):
14+
num = candidates[i]
15+
nums.append(num)
16+
dfs(i, total + num)
17+
nums.pop()
18+
19+
dfs(0, 0)
20+
return result

decode-ways/hi-rachel.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 디코드 가능 1 ~ 26
2+
# 숫자의 첫 번째 자리가 0이라면 decode x
3+
# O(n) time, O(n) space
4+
5+
class Solution:
6+
def numDecodings(self, s: str) -> int:
7+
memo = {len(s): 1} # 문자열 끝에 도달했을 때는 경우의 수 1
8+
9+
def dfs(start):
10+
if start in memo: # 이미 계산한 위치 재계산 x
11+
return memo[start]
12+
if s[start] == "0":
13+
memo[start] = 0
14+
elif start + 1 < len(s) and int(s[start:start + 2]) < 27: # 두 자리로 해석 가능할 때
15+
memo[start] = dfs(start + 1) + dfs(start + 2) # 첫 한 자리만 decode 경우 + 두 자리 한꺼번에 decode 경우
16+
else:
17+
memo[start] = dfs(start + 1) # 두 자리로 decode 불가능할 때 -> 한 자리만 decode
18+
return memo[start]
19+
return dfs(0)

maximum-subarray/hi-rachel.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 최대 부분 배열 합 문제
2+
3+
# O(n^2) time, O(1) space
4+
# Time Limit Exceeded
5+
6+
class Solution:
7+
def maxSubArray(self, nums: List[int]) -> int:
8+
max_total = nums[0]
9+
for i in range(len(nums)):
10+
total = 0
11+
for j in range(i, len(nums)):
12+
total += nums[j]
13+
max_total = max(total, max_total)
14+
return max_total
15+
16+
# 개선 풀이
17+
# O(n) time, O(1) space
18+
class Solution:
19+
def maxSubArray(self, nums: List[int]) -> int:
20+
max_total = nums[0]
21+
total = nums[0]
22+
for i in range(1, len(nums)):
23+
total = max(nums[i], total + nums[i])
24+
max_total = max(total, max_total)
25+
return max_total
26+
27+
# DP 풀이
28+
# O(n) time, O(n) space
29+
class Solution:
30+
def maxSubArray(self, nums: List[int]) -> int:
31+
dp = [0] * len(nums)
32+
dp[0] = nums[0]
33+
for i in range(1, len(nums)):
34+
dp[i] = max(nums[i], dp[i - 1] + nums[i])
35+
return max(dp)

number-of-1-bits/hi-rachel.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# O(log n) time, O(1) space
2+
# % 나머지, // 몫
3+
4+
class Solution:
5+
def hammingWeight(self, n: int) -> int:
6+
cnt = 0
7+
8+
while n > 0:
9+
if (n % 2) == 1:
10+
cnt += 1
11+
n //= 2
12+
13+
return cnt
14+
15+
16+
# O(log n) time, O(log n) space
17+
class Solution:
18+
def hammingWeight(self, n: int) -> int:
19+
return bin(n).count("1")
20+
21+
22+
# TS 풀이
23+
# O(log n) time, O(log n) space
24+
# function hammingWeight(n: number): number {
25+
# return n.toString(2).split('').filter(bit => bit === '1').length;
26+
# };

valid-palindrome/hi-rachel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# O(n) time, O(1) space
2+
# isalnum() -> 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴.
3+
4+
class Solution:
5+
def isPalindrome(self, s: str) -> bool:
6+
l = 0
7+
r = len(s) - 1
8+
9+
while l < r:
10+
if not s[l].isalnum():
11+
l += 1
12+
elif not s[r].isalnum():
13+
r -= 1
14+
elif s[l].lower() == s[r].lower():
15+
l += 1
16+
r -= 1
17+
else:
18+
return False
19+
return True
20+

valid-palindrome/hi-rachel.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// O(n) time, O(1) space
2+
3+
function isPalindrome(s: string): boolean {
4+
let low = 0,
5+
high = s.length - 1;
6+
7+
while (low < high) {
8+
while (low < high && !s[low].match(/[0-9a-zA-Z]/)) {
9+
low++;
10+
}
11+
while (low < high && !s[high].match(/[0-9a-zA-Z]/)) {
12+
high--;
13+
}
14+
if (s[low].toLowerCase() !== s[high].toLowerCase()) {
15+
return false;
16+
}
17+
low++;
18+
high--;
19+
}
20+
return true;
21+
}

0 commit comments

Comments
 (0)