[백준] 15656번 N과 M (7)

거북이·2023년 1월 19일
0

백준[실버3]

목록 보기
27/92
post-thumbnail

💡문제접근

  • 중복순열을 이용하는 문제였다.

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

from itertools import product

N, M = map(int, input().split())
li = list(map(int, input().split()))
li.sort()

for i in product(li, repeat = M):
    print(*i)

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

import sys
input = sys.stdin.readline

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

def recursive():
    if len(res) == M:
        print(" ".join(map(str, res)))
        return 0
    else:
        for i in range(len(li)):
            res.append(li[i])
            recursive()
            res.pop()
recursive()

💡소요시간 : 1m

0개의 댓글