๋ฌธ์
ํ์ด
์ด๊ธฐ ์ฝ๋:
from itertools import combinations, product
def solution(clothes):
answer = 0
dict = {i[1]:[] for i in clothes}
result = []
for name, kind in clothes:
dict[kind].append(name)
dic_values = dict.values()
clothList = list(dic_values)
for i in range(1,len(clothList)+1):
for j in combinations(clothList,i):
# print(list(product(*j)), end="\n")
answer += len(list(product(*j)))
return answer
Python
๋ณต์ฌ
๋ฌธ์ ์ : ์๊พธ ํ
์คํธ์ผ์ด์ค 1๋ฒ์์ ์๊ฐ์ด๊ณผ๊ฐ ๋ธ
๋์๋ฐ์ ์ฝ๋:
# ์คํ์ด๊ฐ ๊ฐ์ง ์์๋ค์ด ๋ด๊ธด 2์ฐจ์ ๋ฐฐ์ดclothes๊ฐ ์ฃผ์ด์ง
# ์๋ก ๋ค๋ฅธ ์ท์ ์กฐํฉ์ ์ return
#์์ด - ์์๋ฅผ ํฌํจํ ์กฐํฉ example) ๋ฐ์ฅ ๋ถ๋ฐ์ฅ
#์กฐํฉ - ์์๋ฅผ ์๊ดํ์ง ์์ ์กฐํฉ ) ํ๊ธ์ธ์ 2 ๋ช
from itertools import combinations, product
def solution(clothes):
answer = 1
dict = {i[1]:["noItems"] for i in clothes}
result = []
for name, kind in clothes:
dict[kind].append(name)
for key,value in dict.items():
answer *= len(value)
answer -= 1
return answer
Java
๋ณต์ฌ
๋ฐฐ์ด์
์ ์ํ ์ฌ๋์ ์ฒ์ฌ์ธ ๊ฒ ๊ฐ๋ค. ์กฐํฉ์ผ๋ก ์ ๊ทผ ํ๋ค ๋ณด๋ฉด ๋ถ๋ช
1๋ฒ ํ
์คํธ ์ผ์ด์ค๊ฐ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค ใ
ใ
,,, ๊ทธ๋์ ๋ค๋ฅธ ์ํ ์์ผ๋ก ์ ๊ทผ์ ํด์ผ ํ๋ค. ๊ทธ๋์ ์๊ฐํ ๊ฒ์ด ์ ์
๋ ๊ฒ๋ ํฌํจ ์ํค๋ ๊ฒ์ด๋ค.
์ด๊ธฐ ์ ๊ทผ์ ์์ 2๊ฐ ํ์ 2๊ฐ ์์ ๊ฒฝ์ฐ ์์๋ง ์
์ ๊ฒฝ์ฐ, ํ์๋ง ์
์ ๊ฒฝ์ฐ๋ฅผ ๋ฐ๋ก combination์ผ๋ก ๊ณ์ฐ ํด์ฃผ์ด์ผ ํ๋ค. ํ์ง๋ง ์ ์
์ ๊ฒฝ์ฐ๋ฅผ ๋ฐ๋ก ์ถ๊ฐํด์ค๋ค๋ฉด?
(์์ 2๊ฐ + ์์
์์๊ฒฝ์ฐ) * (ํ์2๊ฐ + ์์
์์๊ฒฝ์ฐ) - 1(๋๋ค ์์
์์ ๊ฒฝ์ฐ)
์ด๋ ๊ฒ ์ ์
์์ ๊ฒฝ์ฐ๋ ๋ฐฐ์ด์ ํฌํจ ์ํจ๋ค๋ฉด ๋ฐฐ์ด์ 1๊ฐ์ฉ, 2๊ฐ์ฉ ๋๋ฆฌ๋ฉด์ ์กฐํฉ์ ์๊ฐํ ํ์ ์์ด ๊ฐ๋จํ ์ํ์์ผ๋ก ๋น ๋ฅด๊ฒ ๊ฐ๋ฅํ๋ค!!
์ฆ, ์กฐํฉ์ ํ๋ํ๋ ๋ด์ผํ๋๊ฒ์ด ์๋๋ผ๋ฉด! ๊ฒฝ์ฐ์ ์๋ง ์๊ฐํ๋ค๋๊ฒ์ด๋ฉด ์ด๋ฌํ ๊ณต์์ ์ ์ฉํ์ฌ ์ํ์์ผ๋ก ๊ฐ๋จํ๊ฒ ํด๊ฒฐํด๋ณด์.
์ถ๊ฐํ์ต โ ์ธ์๋๊ธฐ!!
์กฐํฉ(Combinations)
์์ด(Permutations)