[Programmers] ๋ฉ๋ด ๋ฆฌ๋ด์ผ (Python)
by ๋๋ถ๋
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋ฉ๋ด ๋ฆฌ๋ด์ผ
๋ ์คํ ๋์ ์ด์ํ๋ ์ค์นดํผ๋ ์ฝ๋ก๋19๋ก ์ธํ ๋ถ๊ฒฝ๊ธฐ๋ฅผ ๊ทน๋ณตํ๊ณ ์ ๋ฉ๋ด๋ฅผ ์๋ก ๊ตฌ์ฑํ๋ ค๊ณ ๊ณ ๋ฏผํ๊ณ ์์ต๋๋ค. ๊ธฐ์กด์๋ ๋จํ์ผ๋ก๋ง ์ ๊ณตํ๋ ๋ฉ๋ด๋ฅผ ์กฐํฉํด์ ์ฝ์ค์๋ฆฌ ํํ๋ก ์ฌ๊ตฌ์ฑํด์
programmers.co.kr
์ด ๋ฌธ์ ๋ ์ ํด์ง ์๋งํผ์ ๋จํ ๋ฉ๋ด ๊ฐฏ์๊ฐ ํฌํจ๋ ์ฝ์ค ์๋ฆฌ๋ฅผ ๋ง๋๋ ๋ฌธ์ ์ ๋๋ค.
๋จ, ์ต์ 2๋ช ์ด์ ์ฃผ๋ฌธํ ์์์ด์ด์ผํ๊ณ , 2๋ช ์ด์ ์ฃผ๋ฌธํ ๋ฉ๋ด ์กฐํฉ์ด์ด์ผ ํฉ๋๋ค.
๋จผ์ combinations๋ฅผ ์ฌ์ฉํด ์ ํด์ง ๋จํ ๋ฉ๋ด ๊ฐฏ์๋งํผ ๋ฉ๋ด ์กฐํฉ์ ์์ฑํ์ฌ ๋ฆฌ์คํธ์ ์ ์ฅํฉ๋๋ค.
๊ทธ ํ Counter๋ฅผ ์ฌ์ฉํด ๋ฉ๋ด ์กฐํฉ ๋ฆฌ์คํธ์์ ๊ฐ๊ฐ์ ๋ฉ๋ด ์กฐํฉ ์๋ฅผ ์ธ์ด์ค๋๋ค.
๊ทธ ์ค ๋ฉ๋ด ์กฐํฉ์ด ๊ฐ์ฅ ๋ง์ด ๋์จ ๊ฐ์ max_๋ก ์ง์ ํด์ค๋๋ค.
max_๊ฐ 2 ์ด์์ด๋ฉด, ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ชจ๋ ๋ง์กฑํ๋ฏ๋ก ์ฝ์ค ์๋ฆฌ๋ก ์ถ๊ฐํฉ๋๋ค.
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
from itertools import combinations
from collections import Counter
def solution(orders, course):
answer = []
for c in course :
temp = []
for order in orders :
combi = combinations(sorted(order), c)
temp += combi
odict = Counter(temp)
if odict :
max_ = max(list(odict.values()))
if max_ >= 2:
for key, value in odict.items() :
if odict[key] == max_ :
answer.append(''.join(key))
return sorted(answer)
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ํ์ผ๋ช ์ ๋ ฌ (Java) (0) | 2021.05.05 |
---|---|
[Programmers] ๊ตฌ๋ช ๋ณดํธ (Java) (0) | 2021.05.01 |
[Programmers] ๋๋ฌผ ์ ๊ตฌํ๊ธฐ (COUNT) (0) | 2021.04.17 |
[Programmers] ์ต์๊ฐ ๊ตฌํ๊ธฐ (MIN) (0) | 2021.04.17 |
[Programmers] ์ด๋ฆ์ด ์๋ ๋๋ฌผ์ ์์ด๋ (MySQL) (0) | 2021.02.07 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
๋๋ถ๋์ Devlog
๋๋ถ๋