Skip to content
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
19 changes: 19 additions & 0 deletions combination-sum/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ans = []
def combination(index, cur_comb, cur_sum):
if cur_sum == target:
ans.append(cur_comb[:])
return
if cur_sum > target or index >= len(candidates):
return
cur_comb.append(candidates[index])
combination(index, cur_comb, cur_sum + candidates[index])
# 합이 target이랑 같던지, 크던지, 길이를 넘던지하면 return으로 탈출
# 그 외에 다른 경우의 수를 봐야하므로
# 마지막꺼는 다시 빼고 어쨌든 index 넘겨서 다시 combination 확인해봐야함
cur_comb.pop()
combination(index + 1, cur_comb, cur_sum)
return ans

return combination(0, [], 0)
30 changes: 30 additions & 0 deletions decode-ways/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def numDecodings(self, s: str) -> int:
memo = {}

def decode(index):
# 이미 계산했으면 바로 반환
if index in memo:
return memo[index]

# 기저 사례
## 끝까지 왔으면 성공
if index == len(s):
return 1
## 0으로 시작하면 불가능
if s[index] == '0':
return 0

# 재귀 계산
ways = decode(index + 1)

if index + 1 < len(s) and int(s[index:index+2]) <= 26:
ways += decode(index + 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

코멘트와 코드만으로도 명확하게 이해되는 깔끔한 풀이인 것 같습니다!!

추가로 현재 코드는 memoization + dfs를 이용한 top-down DP로 decode(index)를 계산하기 위해 decode(index + 1), decode(index + 2)가 필요한데요,

저는 이를 bottom-up DP로 풀이하고, dp[i]를 계산할 때 dp[i - 1], dp[i - 2]만 확인하므로 DP 테이블을 O(1) space 변수 두 개로 대체하여 공간 복잡도를 최적화했습니다!

이렇게도 풀 수 있어서 참고차 공유드려요~!


# 메모이제이션
memo[index] = ways
return ways

# 시간복잡도, 공간복잡도 O(n)

return decode(0)
14 changes: 14 additions & 0 deletions maximum-subarray/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
max_total = nums[0]
total = 0
# subarray는 array에서 서로 인접해야함
# 인접한 값의 합이 마이너스인 경우, 그냥 현재 값만 사용하는게 합보다 큼
# total 이 0보다 작은경우 그냥 0으로 변경
for num in nums:
if total < 0:
total = 0
total += num
max_total = max(total, max_total)
# 시간복잡도 O(n), 공간복잡도 O(1)
return max_total
11 changes: 11 additions & 0 deletions number-of-1-bits/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
# 시간복잡도 O(log n) 반으로 나누기 때문
while n > 0:
if n % 2 == 1:
count += 1
n = n // 2
else:
n = n / 2
Copy link
Contributor

Choose a reason for hiding this comment

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

n & n-1로 오른쪽에서부터 1인 비트를 하나씩 지워가며 개수를 세는 방식인 Brian Kernighan's 알고리즘이라는 방법도 있어 공유드립니다~!

https://leetcode.com/problems/number-of-1-bits/solutions/4341511/faster-lesser-3-methods-simple-count-brian-kernighan-s-algorithm-bit-manipulation-explained

return count
11 changes: 11 additions & 0 deletions valid-palindrome/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
# 시간복잡도 O(n)
# loop
s_list = [x.lower() for x in s if x.isalnum()]
# 시간복잡도 O(n)
# loop + list pop()
for i in range(len(s_list)//2):
if s_list[i] != s_list.pop():
return False
return True