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
25 changes: 25 additions & 0 deletions combination-sum/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:

answer = []

# 재귀
def backtrack(start, path, total):
# total이 target과 같아지면 path복사해서 answer에 추가하고 종료
if total == target:
answer.append(path[:])
return

# total이 target값 넘어가면 종료
if total > target:
return

for i in range(start, len(candidates)):
path.append(candidates[i]) # 일단 path에 추가하고
backtrack(i, path, total + candidates[i]) # 검증하기
path.pop() # 마지막 값 꺼내고 다음으로

# backtrack 함수호출
backtrack(0, [], 0)

return answer
Copy link
Contributor

@clara-shin clara-shin Apr 19, 2025

Choose a reason for hiding this comment

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

path로 리스트를 복사 후 추가하는 부분이 좋았습니다 😀
전체적으로 코드가 간결하고 각 부분에 주석을 달아 로직을 이해하기 쉬웠어요

24 changes: 24 additions & 0 deletions decode-ways/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def numDecodings(self, s: str) -> int:

# DP
dp = [0]*(len(s)+1)

# s가 0으로 시작하면 0 return
if s[0] == '0':
return 0

dp[0] = 1 # 빈문자열은 해석가능한 1가지 경우로 취급 (초기기준점 역할, dp[i-2]계산시필요)
dp[1] = 1 # 첫번째자리의 처리방법은 1가지

# len(s)가 2 이상일때
for i in range(2,len(s)+1):
one = int(s[i-1]) # 한자리(현재자리)
two = int(s[i-2:i]) # 한자리 + 앞자리 = 두자리

if 1 <= one <= 9:
dp[i] += dp[i-1]
if 10 <= two <= 26:
dp[i] += dp[i-2]

return dp[len(s)]
14 changes: 14 additions & 0 deletions maximum-subarray/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# 시간복잡도: O(n) - nums 배열을 한 번만 순회함
# 공간복잡도: O(n) - dp 배열을 nums 길이만큼 생성

# DP
dp = [0]*len(nums)
dp[0] = nums[0] # 초기화

for i in range(1,len(nums)):
# 현재값과 (이전까지의 합 + 현재값) 중 더 큰 값을 dp[i]에 저장
dp[i] = max(nums[i], nums[i]+dp[i-1])

return max(dp)
9 changes: 9 additions & 0 deletions number-of-1-bits/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def hammingWeight(self, n: int) -> int:
answer = 0

while n > 0:
answer += n%2 #나머지(현재 비트가 1이면 ++)
n //= 2 #몫(다음 비트로 이동)

return answer
14 changes: 14 additions & 0 deletions valid-palindrome/yyyyyyyyyKim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def isPalindrome(self, s: str) -> bool:

# 소문자로 변경
s = s.lower()
p = ""

# 문자,숫자만 뽑기
for i in range(len(s)):
if (ord(s[i]) > 96 and ord(s[i]) < 123) or (ord(s[i]) >= 48 and ord(s[i]) <= 57):
p += s[i]

# 문자열 뒤집기
return p == p[::-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

p[::-1]을 활용하여 문자열을 효율적으로 뒤집는 방법을 사용한 점 👍