본문 바로가기
Data Science/알고리즘 공부

[백준] #15650 N과 M(2)

by titaniumm 2020. 3. 10.

[핵심]

메모리를 생각하는 것이 핵심이다. 내장된 모듈을 활용하여 순열이나 조합을 리스트에 저장하면

대부분의 경우 메모리 초과가 된다. 다음와 같이 재귀함수를 활용하여 바로바로 출력을 하는 연습을 하자! (메모리가 작을 것이다.)

 

 

import sys

N,M = map(int,sys.stdin.readline().split())
hi=[False]*N
test = []

def solve(depth,idx,N,M):
    if depth == M:
        print(test)

    for i in range(idx,N):
        if hi[i] == 0:
            test.append(i+1)
            hi[i] = True
            solve(depth+1,i+1,N,M)
            hi[i] = False
            test.pop()

solve(0,0,N,M)

댓글