-
Notifications
You must be signed in to change notification settings - Fork 8
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
[02주차 정지원] 타겟 넘버 #11
base: main
Are you sure you want to change the base?
[02주차 정지원] 타겟 넘버 #11
Conversation
idx = 0 | ||
current = 0 | ||
|
||
def dfs(list_of_nums, target_num, current_num, current_idx): | ||
if current_idx == len(list_of_nums): | ||
return 1 if current_num == target_num else 0 | ||
|
||
# 현재 상황에서 숫자를 빼거나 더하여 재귀함수 호출 | ||
return dfs(list_of_nums, target_num, current_num + list_of_nums[current_idx], current_idx + 1) + dfs(list_of_nums, target_num, current_num - list_of_nums[current_idx], current_idx + 1) | ||
|
||
result = dfs(numbers, target, current, idx) | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
solution()
의 numbers
, target
은 dfs()
안에서도 충분이 참조가 가능하니, 인자를 줄여보아도 괜찮을 것 같아요!
풀이는 전반적으로 아주 나이스하군요! 고생하셨습니다.
def solution(numbers, target):
def count_cases_by_dfs(index = 0, current = 0):
if index == len(numbers):
return 1 if current == target else 0
return count_cases_by_dfs(index+1, current+numbers[index]) + count_cases_by_dfs(index+1, current-numbers[index])
return count_cases_by_dfs()
01주차/정지원/solution.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm.. 지난 주차에 올렸던 파일인데 왜 같이 올라왔을까요... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저 지난주 PR이 인 받아져서 엎쳐서 들어간 거 같아요.. ㅠㅠㅠ 해결법을 몰라서 덮어썼습니자
🔎 문제 소개
🖊️ 풀이
DFS라는 알고리즘의 한 형태를 처음 사용해 보았습니다. 재귀함수를 사용하는 것은 괜찮으나 재귀함수를 사용하는 알고리즘을 어떻게 최적화해야 할 지가 DFS 문제에서 가장 어려웠던 것 같습니다 ㅠㅠ