728x90
반응형
문제 출처 :
https://programmers.co.kr/learn/courses/30/lessons/72411?language=python3
문제 풀이 :
- 카카오 문제 답게 문제를 이해하는데에는 크게 어려움이 없다.
- 다만 구현에는 조금 까다로운 점이 있었다. 나같은경우는 python의 combinations 함수를 이용해서 모든 경우의 수를 다 만들고 주어진 조건에 체크를 하며 풀었다.
- 하지만 이것은 실제로 많은 시간이 소모 되기 때문에 시간초과를 받기 쉽다.
- 그래서 구글링을 하는 과정에서 Python의 Counter 를 알게 되었다.
- 이후 Counter을 이용해서 다음과 같이 풀었다.
1. 특정 조합의 개수에서 가능한 조합들만 만들고 가장 많이 나온 메뉴를 뽑는다.
2. Counter 써서 배열에서 원소가 몇 번 나왔는지 알려준다 이를 이용해서 가장 많이 나온 걸 뽑는다.
- 역시 Python은 아는게 힘이다.
from itertools import combinations
from collections import Counter
def solution(orders, course):
answer = []
for k in course:
candidates = []
for menu_li in orders:
for li in combinations(menu_li, k):
res = ''.join(sorted(li))
candidates.append(res)
sorted_candidates = Counter(candidates).most_common()
answer += [menu for menu, cnt in sorted_candidates if cnt > 1 and cnt == sorted_candidates[0][1]]
return sorted(answer)
반응형
'Computer Science > Algorithm' 카테고리의 다른 글
[백준] 14888 - 연산자 끼워넣기 (Python) (0) | 2021.07.11 |
---|---|
[백준] 14889 - 스타트와 링크 (Python) (0) | 2021.07.11 |
[프로그래머스] 신규 아이디 추천 (Level 1) (Python) (0) | 2021.07.03 |
[백준] 14499 - 주사위 굴리기 (Python) (0) | 2021.07.03 |
[백준] 13458 - 시험 감독 (Python) (0) | 2021.07.03 |