인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

pku9281643's profile image
pku9281643

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

6. Finding duplicate permutations (DFS)

dfs 인자 관련 질문

Written on

·

189

0

안녕하세요, 선생님 dfs도 강의 잘 보고 있습니다.

다름이 아니라 다음과 같이 res를 사용안하고 dfs 인자로 tuple을 계속 넘겨줬습니다. 재귀함수에서 다음과 같이 list나 tuple을 넘겨주면 메모리에 문제가 생길거 같은데 시간상에도 문제가 생길까요?

import sys
sys.stdin = open('section6/input.txt''rt')
n, m = map(intinput().split())
cnt = 0
def dfs(tp):
    global cnt
    if len(tp)==m:
        print(' '.join(list(map(str, tp))))
        cnt += 1
        return
    for i in range(1, n+1):
        dfs(tp+(i,))
for i in range(1, n+1):
    dfs((i,))
print(cnt)

python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

위와 같은 방법을 좋아하시면 어쩔수 없지만 저는 별로 권하고 싶지는 않는 방법입니다.

재귀 문제는 영상에서 하는 방법처럼 리스트와 L변수로 컨트롤 하는 법에 익숙해지시는게 좋습니다. 

pku9281643's profile image
pku9281643

asked

Ask a question