코딩테스트 고득점 Kit - 완전탐색 2 (Level 2)

 

https://programmers.co.kr/learn/courses/30/lessons/42839


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from itertools import permutations
from math import sqrt

def solution(numbers):
    numbers = [str(n) for n in numbers]

    all_numbers = set()
    for i in range(1, len(numbers)+1):
        for p in permutations(numbers, i):
            all_numbers.add(int(''.join(list(p))))
    all_numbers = list(all_numbers)


    answer = 0
    for e in all_numbers:
        if e < 2:
            continue

        for i in range(2, int(sqrt(e)) + 1):
            if e % i == 0:
                break
        else:
            answer += 1

    return answer

Complexity

$O(n!)$

  • $n$ = len(numbers)