๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Python

[์•Œ๊ณ ๋ฆฌ์ฆ˜/Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์œ„์žฅ (ํ•ด์‹œ)

by Danna 2021. 3. 11.
728x90
728x90

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

 

์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฒ• ๐Ÿ‘€

์—ฌ๋Ÿฌ ์˜์ƒ๋ผ๋ฆฌ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ณฑ์ง‘ํ•ฉ์„ ์ƒ๊ฐํ–ˆ๋‹ค.

 

๊ณฑ์ง‘ํ•ฉ (๋ฐ์นด๋ฅดํŠธ ๊ณฑ)์€ ๊ฐ ์ง‘ํ•ฉ์˜ ์›์†Œ๋ฅผ ๊ฐ ์„ฑ๋ถ„์œผ๋กœ ํ•˜๋Š” ํŠœํ”Œ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ A x B ๋ผ๊ณ  ํ•œ๋‹ค. ๊ณฑ์ง‘ํ•ฉ์˜ ๊ฐœ์ˆ˜๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ A x B ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ์ž๊ธฐ ์ž์‹ ๋งŒ์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— (A+1) x (B+1) - 1 ๋กœ ๊ณ„์‚ฐํ–ˆ๋‹ค. 1 ์„ ๋นผ์ฃผ๋Š” ์ด์œ ๋Š” (A+1) x (B+1) ์˜ ๊ฒฝ์šฐ์—๋Š” ๋น„์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ธฐ์กด ๋ฆฌ์ŠคํŠธ = [a], [b], [c]
์ตœ์ข… ๊ฒฐ๊ณผ = [a], [b], [c], [a, b], [a, c], [b, c], [a, b, c]

 

ํ’€์ด1 (์‹œ๊ฐ„์ดˆ๊ณผ)

solution ํ•จ์ˆ˜

  • clothes : ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด
  • return : ์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜
  1. Key: ์ข…๋ฅ˜, Value: ์ด๋ฆ„ ์œผ๋กœ Dictionary ๋งŒ๋“ค๊ธฐ
    • ์ž๊ธฐ ์ž์‹ ๋งŒ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๊ณ„์‚ฐํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0 ์„ ํฌํ•จํ–ˆ๋‹ค.
  2. ๊ณฑ์ง‘ํ•ฉ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    • ๊ฐœ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ์ง€ ๋ชฐ๋ผ์„œ itertools.product ํ•จ์ˆ˜๋กœ ์ „์ฒด ์ผ€์ด์Šค๋ฅผ ๊ตฌํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋˜์—ˆ๋‹ค.
from itertools import product

def solution(clothes):

    # Key: ์ข…๋ฅ˜, Value: ์ด๋ฆ„ ์œผ๋กœ Dictionary ๋งŒ๋“ค๊ธฐ 
    clothes_dict = {}
    for c in clothes:
        key = c[1]
        if not key in clothes_dict:
            clothes_dict[key] = [0]
        clothes_dict[key].append(c[0])
        
    # ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    cases = len(list(product(*clothes_dict.values()))) - 1
    return cases

ํ’€์ด2 (ํ†ต๊ณผ ํ’€์ด)

  • ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๊ตฌํ•˜์ง€ ์•Š๊ณ  ๊ฐœ์ˆ˜๋งŒ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
    • ํ•˜๋‚˜์”ฉ ์ž…๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จํ•˜๊ธฐ ์œ„ํ•ด 1 ์„ ๋”ํ•œ๋‹ค.
    • ๊ณฑ์ง‘ํ•ฉ(?) ๊ฐœ์ˆ˜ = (A+1) * (B+1) - 1
  • dict value ๋ฆฌ์ŠคํŠธ์— 0 ์„ ์ถ”๊ฐ€ํ–ˆ๋˜ ๊ฑธ, ๊ฐœ์ˆ˜์—์„œ 1 ์„ ๋”ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟจ๋‹ค.
def solution(clothes):

    # Key: ์ข…๋ฅ˜, Value: ์ด๋ฆ„ ์œผ๋กœ Dictionary ๋งŒ๋“ค๊ธฐ
    clothes_dict = {}
    for c in clothes:
        key = c[1]
        if not key in clothes_dict:
            clothes_dict[key] = []
        clothes_dict[key].append(c[0])
    
    # ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    cases = 1
    for v in clothes_dict.values():
        cases *= len(v) + 1
    return cases - 1

 

ํ’€์ด3 (Counter ์ด์šฉ)

  • ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  Counter ๋ฅผ ์ด์šฉํ•ด๋ดค๋‹ค.
  • collections.Counter ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ ์ข…๋ฅ˜๋งˆ๋‹ค ๊ฐœ์ˆ˜๋งŒ ์ €์žฅํ•œ๋‹ค.
  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ณผ์ •์ด ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ์ง€ ํ’€์ด 2 ๋ณด๋‹ค ์‹œ๊ฐ„์€ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.
from collections import Counter

def solution(clothes):

    # Key: ์ข…๋ฅ˜, Value: ๊ฐœ์ˆ˜๋กœ Dictionary ๋งŒ๋“ค๊ธฐ 
    count = Counter([kind for name, kind in clothes])
    
    # ๊ณฑ์ง‘ํ•ฉ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    cases = 1
    for v in count.values():
        cases *= v + 1
    return cases - 1

Counter ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ count ๋ณ€์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

clothes = [
	["yellowhat", "headgear"], 
	["bluesunglasses", "eyewear"], 
	["green_turban", "headgear"]
]

Counter({'headgear': 2, 'eyewear': 1}

 

 

โœ”๏ธŽ ํ’€์ด 2 ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ

๋”๋ณด๊ธฐ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)

โœ”๏ธŽ ํ’€์ด 3 ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ

๋”๋ณด๊ธฐ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)

programmers.co.kr/learn/courses/30/lessons/42578

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์œ„์žฅ

 

programmers.co.kr

 

728x90
728x90