ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 5568 ์นด๋“œ ๋†“๊ธฐ with Python
    PS 2021. 8. 31. 22:24
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 5568 ์นด๋“œ ๋†“๊ธฐ

    ๐Ÿ’ก ์กฐ๊ฑด ๋ฐ ํ’€์ด

    1. ์นด๋“œ์˜ ์žฅ์ˆ˜๋Š” 4 <= N <= 10
    2. ๊ฐ ์นด๋“œ์— ์ˆซ์ž๊ฐ€ ์ ํ˜€์žˆ๋‹ค. 1 <= ์ˆซ์ž <= 99
    3. N๊ฐœ์˜ ์นด๋“œ ์ค‘์—์„œ K๊ฐœ์˜ ์นด๋“œ๋ฅผ ๊ณจ๋ผ์„œ ๋ช‡ ๊ฐ€์ง€ ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ?
    4. ๋ธŒ๋ฃจํŠธํฌ์Šค ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    from itertools import permutations as p
    
    arr = []
    n, k = int(stdin.readline()), int(stdin.readline())
    for i in range(n):
        arr.append(int(stdin.readline()))
    
    res = set()
    for i in list(p(arr, k)):
        res.add(''.join(list(map(str, i))))
    
    print(len(res))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    6
    3
    72
    2
    12
    7
    2
    1

    ์‹คํ–‰๊ฒฐ๊ณผ

    68

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. from itertools import permutaions
      permutations๋ผ๋Š” ์ˆœ์—ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
      ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ทธ๋งŒํผ์˜ ๊ฐœ์ˆ˜๋Œ€๋กœ ์ˆซ์ž๋ฅผ ๊บผ๋‚ธ๋‹ค.
    2. ๋ฆฌ์ŠคํŠธ์—๋Š” ๊ฐ™์€ ์ˆซ์ž๋„ ์กด์žฌํ•˜๋ฉฐ, ์ถฉ๋ถ„ํžˆ ์ˆœ์—ด๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ, ์ค‘๋ณต์ด ์กด์žฌํ•œ๋‹ค.
    3. ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•  set() ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“  ํ›„
    4. for ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์—ด๋ฆฌ์ŠคํŠธ์— ์ฐจ๋ก€๋Œ€๋กœ ์ ‘๊ทผํ•˜๋ฉด์„œ, ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด์„œ ํ•ฉ์นœ๋‹ค.
    5. ํ•ฉ์ณ์ง„ ๋ฌธ์ž์—ด์„ set() ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋งŒ๋“  ๋ณ€์ˆ˜์— ์ถ”๊ฐ€ํ•œ๋‹ค.
      • ์ค‘๋ณต์œผ๋กœ ์ถ”๊ฐ€๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค
    6. set() ๋ณ€์ˆ˜์˜ ๊ธธ์ด๋ฅผ ์žฌ์„œ ์ถœ๋ ฅ.

    ๐Ÿ’พ ๋Š๋‚€์ 

    • ์ฒ˜์Œ์—”, ์ˆœ์—ด ๋ฆฌ์ŠคํŠธ๋กœ๋งŒ ๋งŒ๋“ค์–ด์„œ ๊ธธ์ด๋ฅผ ์Ÿ€๋‹ค. -> TLE
    • ์ˆœ์—ด ๋ฆฌ์ŠคํŠธ์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•ด์„œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด ์กฐํ•ฉ์„ ๋ณธ ๋’ค
      ํ’€์–ด๋‚˜๊ฐ€๋Š” ์•„์ด๋””์–ด, set() ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆฐ ๋’ค
      ๋ฐ”๋กœ ํ’€์—ˆ๋‹ค.
    • ๋น„๊ต์  ์‹ค๋ฒ„ ๋ฌธ์ œ๊ฐ€ ์‰ฝ๊ณ  ๊ฐœ๋…์„ ์Œ“๊ธฐ์— ์ข‹์€ ๋ฌธ์ œ๋“ค์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.