๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์†Œ์ˆ˜ ์ฐพ๊ธฐ/Python - Lv.2

by The Future Engineer, Lucy 2024. 10. 25.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

์„ฑ๋Šฅ ์š”์•ฝ

์ฝ”๋“œ 1 โžก๏ธ ๋ฉ”๋ชจ๋ฆฌ: 280 MB, ์‹œ๊ฐ„: 860.28 ms

์ฝ”๋“œ 2 โžก๏ธ ๋ฉ”๋ชจ๋ฆฌ: 10.4 MB, ์‹œ๊ฐ„: 2639.75 ms

๋ฌธ์ œ ์„ค๋ช…

ํ•œ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ํฉ์–ด์ง„ ์ข…์ด ์กฐ๊ฐ์„ ๋ถ™์—ฌ ์†Œ์ˆ˜๋ฅผ ๋ช‡ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์ข…์ด ์กฐ๊ฐ์— ์ ํžŒ ์ˆซ์ž๊ฐ€ ์ ํžŒ ๋ฌธ์ž์—ด numbers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ข…์ด ์กฐ๊ฐ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • numbers๋Š” ๊ธธ์ด 1 ์ด์ƒ 7 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • numbers๋Š” 0~9๊นŒ์ง€ ์ˆซ์ž๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • "013"์€ 0, 1, 3 ์ˆซ์ž๊ฐ€ ์ ํžŒ ์ข…์ด ์กฐ๊ฐ์ด ํฉ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbers return
"17" 3
"011" 2

โœ๐Ÿปํ’€์ด

์ฝ”๋“œ1 ํ’€์ด
permutations๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ์ˆซ์ž์˜ ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค.
0๊ณผ 1์„ ์ง‘ํ•ฉ์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

์ฝ”๋“œ2 ํ’€์ด
๊ฐ€๋Šฅํ•œ ์ˆซ์ž์˜ ์กฐํ•ฉ์„ ๋ชจ๋‘ ์ฐพ๋Š”๋‹ค.
ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ์†Œ์ˆ˜๋ผ๋ฉด ์ง‘ํ•ฉ์— ๋„ฃ๋Š”๋‹ค.

๐Ÿ’ป์ฝ”๋“œ

# permutations๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด
from itertools import permutations

def solution(numbers):
    nums = set()
    for i in range(len(numbers)): # ๊ฐ€๋Šฅํ•œ ์ˆซ์ž ์กฐํ•ฉ ์ฐพ๊ธฐ
        nums |= set(map(int, map("".join, permutations(list(numbers), i + 1))))
    nums -= set(range(0, 2)) # 0๊ณผ 1์„ ์ œ๊ฑฐ
    for i in range(2, int(max(nums) ** 0.5) + 1): # ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
        nums -= set(range(i * 2, max(nums)+ 1, i)) # i์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ œ๊ฑฐ

    return len(nums)
# ๋ฐฑํŠธ๋ž˜ํ‚น ๊ฐ™์ง€ ์•Š์€ ๋ฐฑํŠธ๋ž˜ํ‚น ํ’€์ด
nums = set()

def isprime(num):
    if num == 0 or num == 1:
        return False
    i = 2
    for i in range(2, num):
        if num % i == 0:
            return False

    return True

def backtracking(length, numbers, visit, num):
    if len(num) == length:
        if isprime(int(num)):
            nums.add(int(num))
        return

    for i in range(len(numbers)):
        if not visit[i]:
            visit[i] = True
            backtracking(length, numbers, visit, num + numbers[i])
            visit[i] = False

def solution(numbers):
    visit = [False] * len(numbers)

    for i in range(1, len(numbers) + 1):
        for j in range(len(numbers)):
            if not visit[j]:
                visit[j] = True
                backtracking(i, numbers, visit, numbers[j])
                visit[j] = False

    return len(nums)

๐Ÿ’ก์ƒˆ๋กœ ๋ฐฐ์šด ๋‚ด์šฉ

|= ์—ฐ์‚ฐ์ž

  • '|'๋Š” or์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ์ฆ‰, union์„ ๋œปํ•œ๋‹ค.
  • '|='์€ ํ˜„์žฌ ์ง‘ํ•ฉ์— ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

์ˆซ์ž ** 0.5 โžก๏ธ ์ œ๊ณฑ๊ทผ ํ‘œํ˜„

permutations ์ˆœ์—ดํ•จ์ˆ˜

  • permutations(๋ฆฌ์ŠคํŠธ, ์„ ํƒํ•  ์›์†Œ ์ˆ˜)

combinations ์กฐํ•ฉํ•จ์ˆ˜

  • combinations(๋ฆฌ์ŠคํŠธ, ์„ ํƒํ•  ์›์†Œ ์ˆ˜)

permutations, combinationsํ•จ์ˆ˜ ์‚ฌ์šฉ ์‹œ itertools๋กœ๋ถ€ํ„ฐ importํ•ด์„œ ์‚ฌ์šฉ.

๐Ÿ“ํ›„๊ธฐ

์ „์— c++๋กœ ํ’€์—ˆ์„ ๋•Œ permutatio ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ c++๋กœ ํ’€ ๋•Œ permutation์“ฐ๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋„ˆ๋ฌด ๊ฒช์—ˆ๋”๋‹ˆ permutations๋ฅผ ์“ธ ์ƒ๊ฐ์„ ๋ชป ํ–ˆ๋‹ค..

728x90
๋ฐ˜์‘ํ˜•