코딩테스트 연습 - 메뉴 리뉴얼
레스토랑을 운영하던 스카피는 코로나19로 인한 불경기를 극복하고자 메뉴를 새로 구성하려고 고민하고 있습니다. 기존에는 단품으로만 제공하던 메뉴를 조합해서 코스요리 형태로 재구성해서
programmers.co.kr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
from collections import defaultdict
from itertools import combinations
def solution(orders, course):
answer = []
d = defaultdict(int)
for n in course:
for order in orders:
temp = list(map(list, combinations(order, n)))
for i in temp:
i.sort() ## WX -> WX, XW -> WX
k = ''.join(i)
d[k] += 1
result = sorted(d.items(), key=lambda x: -x[1])
if len(result) >= 1:
max_count = result[0][1]
if max_count >= 2:
for cou, count in result:
if count == max_count:
answer.append(cou)
d = defaultdict(int)
return sorted(answer)
orders = ["XYZ", "XWY", "WXA"]
course = [2, 3, 5]
print(solution(orders, course))
|
cs |
문제에 대한 힌트는 https://tech.kakao.com/2021/01/25/2021-kakao-recruitment-round-1/ 에서 확인 가능하다.
유의할 점:
line 12
dict에 문자열을 추가하면 'WX'와 'XW'가 서로 다른 key로 들어갈 수 있다. 이를 방지하기 위해 문자열을 sort하고 dict에 추가했다.
line 18
5개의 메뉴가 있는 코스를 만들 수 없는 orders가 주어졌다. 이 때 result는 빈 list인데, 존재하지 않는 result의 인덱스를 참조하면 예외가 발생할 수 있다. 그래서 len(result) >= 1 이라는 if문을 추가했다.
'알고리즘 문제풀이 with 파이썬 > 프로그래머스' 카테고리의 다른 글
[스택] 주식 가격 (0) | 2021.10.02 |
---|---|
[큐] 다리를 지나는 트럭 (0) | 2021.10.02 |
[구현] 거리두기 확인하기 (0) | 2021.09.26 |
[문자열] 뉴스 클러스트링 (0) | 2021.09.21 |
[해시] 전화번호 목록 (0) | 2021.09.07 |