[python]ํ๋ก๊ทธ๋๋จธ์ค - ๋ฉ๋ด ๋ฆฌ๋ด์ผ
https://school.programmers.co.kr/learn/courses/30/lessons/72411
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
from itertools import combinations
from collections import Counter
def solution(orders, course):
answer = []
for c in course:
temp = []
for o in orders:
menu = combinations(sorted(o),c)
temp += menu
# print(temp)
counter = Counter(temp)
# print(counter)
# print(len(counter))
if counter:
max_val = max(list(counter.values()))
if max_val >= 2:
for key,value in counter.items():
if counter[key] == max_val:
answer.append(''.join(key))
return sorted(answer)
์ ํด์ง ์๋งํผ ๋จํ ๋ฉ๋ด ๊ฐฏ์๊ฐ ํฌํจ๋ ์ฝ์ค ์๋ฆฌ๋ฅผ ๋ง๋ค์ด์ผ ํจ.
์ต์ 2๋ช ์ด์์ด ์ฃผ๋ฌธํ ์์์ด์ด์ผ ํ๊ณ , 2๋ช ์ด์ ์ฃผ๋ฌธํ ๋ฉ๋ด ์กฐํฉ์ด์ด์ผ ํจ.
combination,Counter ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด ๋ถ๋ฌ์ ํ๋ฉด ๋๋ค!
1. combination์ ์ฌ์ฉํด ์ ํด์ง ๋จํ ๋ฉ๋ด ๊ฐฏ์๋งํผ ๋ฉ๋ด ์กฐํฉ์ ์์ฑํด ๋ฆฌ์คํธ์ ์ ์ฅ
2. Counter๋ฅผ ์ฌ์ฉํด ๋ฉ๋ด ์กฐํฉ ๋ฆฌ์คํธ์์ ๊ฐ๊ฐ์ ๋ฉ๋ด ์กฐํฉ์ ์ธ๊ธฐ
3. ๋ฉ๋ด ์กฐํฉ์ด ๊ฐ์ฅ ๋ง์ด ๋์จ ๊ฐ์ max_val๋ก ์ ์ฅ
4. 2 ์ด์ ์ฃผ๋ฌธํ ๋ฉ๋ด ์กฐํฉ์ด์ด์ผ ํ๋ฏ๋ก max_val์ด 2 ์ด์์ด๋ฉด, ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ฏ๋ก answer์ ์ถ๊ฐ
'Problem Solving > PROGRAMMERS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python]ํ๋ก๊ทธ๋๋จธ์ค - ํํ (0) | 2022.08.01 |
---|---|
[python]ํ๋ก๊ทธ๋๋จธ์ค - ์คํ์ฑํ ๋ฐฉ (0) | 2022.07.29 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ๋น๋ฐ์ง๋ (0) | 2022.07.25 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์ (0) | 2022.07.12 |
[python]ํ๋ก๊ทธ๋๋จธ์ค - ์ ๊ท ์์ด๋ ์ถ์ฒ (0) | 2022.07.07 |
๋๊ธ