Skip to content

[chordpli] Week3 Solutions #1808

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 5 commits into from
Aug 10, 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
29 changes: 29 additions & 0 deletions combination-sum/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import List


class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []
candidates.sort()
self.__backtrack__(candidates, 0, target, result, [])
return result

def __backtrack__(self, candidates: List[int], current_idx: int, target: int, result: List[List[int]],
current_arr: List[int]):
if current_idx >= len(candidates):
return

add_value = sum(current_arr) + candidates[current_idx]
Copy link
Contributor

Choose a reason for hiding this comment

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

매번 sum(current_arr)을 계산하면 비효율적이기에 현재 합계를 변수로 관리하는 방법 추천드립니다


if add_value == target:
current_arr.append(candidates[current_idx])
result.append(current_arr.copy())
current_arr.pop()

if add_value > target:
Copy link
Contributor

Choose a reason for hiding this comment

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

이 조건 이후 바로 return해서 다음 인덱스로 넘어가는 아이디어는 좋으나 너무 깊은 재귀가 발생할 수 있어 주의가 필요합니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오.. 감사합니다! 비슷한 상황이 있을 때, 참고해서 회피하도록 하겠습니다!

return self.__backtrack__(candidates, current_idx + 1, target, result, current_arr)

current_arr.append(candidates[current_idx])
self.__backtrack__(candidates, current_idx, target, result, current_arr)
current_arr.pop()
self.__backtrack__(candidates, current_idx + 1, target, result, current_arr)
3 changes: 3 additions & 0 deletions number-of-1-bits/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count('1')
13 changes: 13 additions & 0 deletions valid-palindrome/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import re


class Solution:
def isPalindrome(self, s: str) -> bool:
lower_s = s.lower()
lower_eng_str = re.sub(r"[^a-z0-9]", "", lower_s)
reverse_str = lower_eng_str[::-1]

if lower_eng_str == reverse_str:
return True

return False