[백준] 15657번 N과 M (8)

거북이·2023년 1월 19일
0

백준[실버3]

목록 보기
23/92
post-thumbnail

💡문제접근

  • 중복조합과 정렬을 이용하는 문제였다.

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

from itertools import combinations_with_replacement

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

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

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

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(start):
    if len(res) == M:
        print(" ".join(map(str, res)))
        return 0
    else:
        for i in range(start, len(li)):
            res.append(li[i])
            recursive(i)
            res.pop()
recursive(0)

💡소요시간 : 1m

0개의 댓글