Algorithm/Programmers
[ํ๋ก๊ทธ๋๋จธ์ค] ํด์ - ์์ฅ (level.2)
๊ฐ์ ๐ฅ
2021. 9. 9. 19:46
๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://programmers.co.kr/learn/courses/30/lessons/42578
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์์ฅ
programmers.co.kr
๋์ํ์ด
- (a์ข
๋ฅ์ ์ท ๊ฐ์ x b์ข
๋ฅ์ ์ท ๊ฐ์ x c์ข
๋ฅ)์ ์ท ๊ฐ์ ๋ก ๊ตฌํ๋๋ฐ, ์ด๋ ๊ฒํ๊ฒ ๋๋ฉดabc ๋ชจ๋ ์
๊ฒ๋๋ ๊ฒฝ์ฐ์ ์๋ง ๊ณ์ฐ๋๋ค. ๋ฐ๋ผ์ a์ ์ท์๊ฑธ์น์ง ์๋ ์ทx์ ์ข
๋ฅ๊ฐ ํ๊ฐ์ง ํฌํจ๋์ด์๋ค๊ณ ์๊ฐํด์ผ a๋ ์์
๊ณ b์c๋ง์ ์
๋ ๊ฒฝ์ฐ์ ์ ๊น์ง ๊ณ์ฐ๋๋ค.
๋ฐ๋ผ์ {(a์ข ๋ฅ์ ์ท ๊ฐ์ + 1) x (b์ข ๋ฅ์ ์ท ๊ฐ์ + 1) x (c์ข ๋ฅ์ ์ท ๊ฐ์ + 1)} ๋ก ๊ณ์ฐํด์ผํ๋ค. ์ด๊ฒ์ ์๊ฐํด๋ด๋๋ฐ ์กฐ๊ธ ์๊ฐ์ด ๊ฑฐ๋ ธ๋๊ฒ ๊ฐ๋ค. (์๋ ๊ฒฝ์ฐ์์ ๊ณ์ฐ์ ์ข ์ฝํ๋คใ ใ ) - ๋ง์ง๋ง์ total -1 ์ ์งํํ๋ ์ด์ ๋ abc ์ค์์๋ ๊ผญ ํ๋ ์ด์์ ๊ฑธ์ณ์ผ ํ๊ธฐ ๋๋ฌธ์ ๋ชจ๋ ์ท์ ๊ฑธ์น์ง ์์ ๊ฒฝ์ฐ ํ๊ฐ์ง๋ฅผ ๋นผ์ค๊ฒ์ด๋ค.
def solution(clothes):
dic = {}
for cloth in clothes: #์ข
๋ฅ๋ณ๋ก dic์ผ๋ก ๋ฌถ์ด์ฃผ๊ธฐ
if cloth[1] in dic:
dic[cloth[1]].append(cloth[0])
else:
dic[cloth[1]] = [cloth[0]]
total = 1 # ์ข
๋ฅ+1 = ์ทx๊ฒฝ์ฐ๊น์ง ์ถ๊ฐํด์ ๊ณ์ฐ
for d in dic:
total *= len(dic[d])+1
return total -1 #๋ชจ๋ ์ทx์ธ๊ฒฝ์ฐ ๋นผ์ค์ผํจ
๋ค๋ฅธ์ฌ๋ ํ์ด
https://programmers.co.kr/learn/courses/30/lessons/42578/solution_groups?language=python3&type=all
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
def solution(clothes):
from collections import Counter
from functools import reduce
cnt = Counter([kind for name, kind in clothes])
answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1
return answer
๋ง์์ฌ๋๋ค์ด Counter ์ ์ฌ์ฉํ๋ค. Counter ํจ์๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ตํ๋ ๊ฒ์ด ํ์ํ๋ค. ๋๋ itertools ํจ์ ํ์ฉ๋๊ฐ ๋ฎ๋ค ใ ใ ์ด ํจ์ ํ์ฉ๋๋ฅผ ๋์ผ ์ ์๋๋ก ์ถ๊ฐ ๊ณต๋ถ๋ฅผ ์งํํด์ผ ๊ฒ ๋ค.
๋ฐ์ํ