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 : ์๋ก ๋ค๋ฅธ ์ท์ ์กฐํฉ์ ์
- Key: ์ข
๋ฅ, Value: ์ด๋ฆ ์ผ๋ก Dictionary ๋ง๋ค๊ธฐ
- ์๊ธฐ ์์ ๋ง ํฌํจํ๋ ๊ฒฝ์ฐ๋ ๊ณ์ฐํด์ผํ๊ธฐ ๋๋ฌธ์ 0 ์ ํฌํจํ๋ค.
- ๊ณฑ์งํฉ ๊ฐ์ ๊ตฌํ๊ธฐ
- ๊ฐ์๋ฅผ ์ด๋ป๊ฒ ๊ตฌํ ์ง ๋ชฐ๋ผ์ 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
728x90
728x90
'๐ Algorithm > ์๊ณ ๋ฆฌ์ฆ-Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ/Python] BOJ 1543 ๋ฌธ์ ๊ฒ์ (0) | 2021.04.22 |
---|---|
[์๊ณ ๋ฆฌ์ฆ/Python] BOJ 1182 ๋ถ๋ถ์์ด์ ํฉ (0) | 2021.04.21 |
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ๋คํธ์ํฌ (DFS) (0) | 2021.03.06 |
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ํ๊ฒ ๋๋ฒ (DFS) (0) | 2021.03.06 |
[์๊ณ ๋ฆฌ์ฆ/Python] BOJ 1158 - ์์ธํธ์ค ๋ฌธ์ (๊ธฐ์ด ์๋ฃ๊ตฌ์กฐ) (2) | 2021.03.06 |