Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jiwonsudo
Copy link
Contributor

🔎 문제 소개

🖊️ 풀이

  • 시간 복잡도:
  • 입력의 크기:
    • (예시) 2 <= $N <= 20

DFS라는 알고리즘의 한 형태를 처음 사용해 보았습니다. 재귀함수를 사용하는 것은 괜찮으나 재귀함수를 사용하는 알고리즘을 어떻게 최적화해야 할 지가 DFS 문제에서 가장 어려웠던 것 같습니다 ㅠㅠ

Comment on lines +2 to +13
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
Copy link
Member

Choose a reason for hiding this comment

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

solution()numbers, targetdfs()안에서도 충분이 참조가 가능하니, 인자를 줄여보아도 괜찮을 것 같아요!

풀이는 전반적으로 아주 나이스하군요! 고생하셨습니다.

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()

Copy link
Member

Choose a reason for hiding this comment

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

hmm.. 지난 주차에 올렸던 파일인데 왜 같이 올라왔을까요... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

저 지난주 PR이 인 받아져서 엎쳐서 들어간 거 같아요.. ㅠㅠㅠ 해결법을 몰라서 덮어썼습니자

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants