Skip to content

Commit 5005c96

Browse files
authored
[freemjstudio] WEEK 03 Solutions (#2734)
* Add week 03 solutions * freemjstudio/decode-ways * Fix decode ways formatting * freemjstudio/maximum-subarray
1 parent 6edaca7 commit 5005c96

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

combination-sum/freemjstudio.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
answer = []
4+
5+
def backtracking(start_idx, total, path):
6+
if target == total:
7+
answer.append(path[:]) # 새로운 리스트 객체를 만들어서 저장함.
8+
# 리스트가 mutable 객체이므로 path[:] 로 복사하지 않으면 path 가 바뀔 때 answer 안의 값도 바뀌게 된다.
9+
return
10+
11+
if target < total:
12+
return
13+
14+
for idx in range(start_idx, len(candidates)):
15+
new_node = candidates[idx] # 동일한 숫자 재사용 가능함. start_idx 도 포함한다.
16+
path.append(new_node)
17+
18+
backtracking(idx, total + new_node, path)
19+
# 선택 취소
20+
path.pop()
21+
22+
backtracking(0, 0, [])
23+
return answer

decode-ways/freemjstudio.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
# invalid case
4+
if s[0] == "0":
5+
return 0
6+
7+
dp = [0] * (len(s)+1) # 앞에서부터 i개 문자를 해석하는 방법
8+
9+
dp[0] = 1
10+
dp[1] = 1
11+
for i in range(2, len(s)+1):
12+
one_digit = s[i-1]
13+
two_digits = int(s[i-2]) * 10 + int(s[i-1])
14+
if one_digit != '0': # 현재 숫자를 한자리로 해석하는 경우. (0 은 매칭되는 알파벳이 없으므로 제외)
15+
dp[i] += dp[i-1]
16+
17+
if 10 <= two_digits <= 26: # 현재 숫자를 바로 앞자리 숫자와 묶어서 두자리로 해석하는 경우
18+
dp[i] += dp[i-2]
19+
20+
return dp[len(s)]

maximum-subarray/freemjstudio.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n == 1:
5+
return nums[0]
6+
7+
dp = [0] * n
8+
dp[0] = nums[0]
9+
# dp[i] = i에서 끝나는 부분 배열의 최대 합
10+
11+
for i in range(1, n):
12+
dp[i] = max(nums[i], dp[i-1] + nums[i])
13+
14+
return max(dp)

number-of-1-bits/freemjstudio.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
binary = bin(n)
4+
return binary.count("1")
5+
6+
# bin() 을 못쓰는 경우의 풀이
7+
class Solution:
8+
def hammingWeight(self, n: int) -> int:
9+
count = 0
10+
11+
while n > 0:
12+
count += n % 2
13+
n //=2
14+
15+
return count

valid-palindrome/freemjstudio.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
characters = "abcdefghijklmnopqrstuvwxyz0123456789"
4+
s = s.lower()
5+
6+
filtered = ""
7+
for ch in s:
8+
if ch in characters:
9+
filtered += ch
10+
11+
n = len(filtered)
12+
left = 0
13+
right = n-1
14+
while left <= n//2 and right >= n//2:
15+
if filtered[left] == filtered[right]:
16+
left += 1
17+
right -= 1
18+
else:
19+
return False
20+
21+
return True

0 commit comments

Comments
 (0)