[백준] 15652번 N과 M (4)

거북이·2023년 1월 18일
0

백준[실버3]

목록 보기
14/92
post-thumbnail

💡문제접근

중복조합을 이용하는 문제였다.

💡코드(메모리 : 30616KB, 시간 : 48ms)

from itertools import combinations_with_replacement

N, M = map(int, input().split())
arr = [i for i in range(1, N+1)]

for i in combinations_with_replacement(arr, M):
    print(*i)

📌백트래킹 풀이 방법(메모리 : 31256KB, 시간 : 56ms)

import sys
input = sys.stdin.readline

N, M = map(int, input().strip().split())
li = []

def recursive(start):
    if len(li) == M:
        print(" ".join(map(str, li)))
        return 0
    else:
        for i in range(start, N+1):
            li.append(i)
            recursive(i)
            li.pop()
recursive(1)

💡소요시간 : 1m

0개의 댓글