Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 혼공머신
- 추천시스템
- 팀 분석
- 삼성 SDS Brigthics
- 영상제작기
- 직원 이직여부
- Brightics를 이용한 분석
- 삼성SDS Brigthics
- Brigthics Studio
- 데이터 분석
- 혼공
- Brightics Studio
- Brigthics를 이용한 분석
- 혼공학습단
- 개인 의료비 예측
- 포스코 청년
- 혼공머신러닝딥러닝
- 삼성 SDS
- 노코드AI
- Brigthics
- 포스코 아카데미
- 데이터분석
- 삼성SDS Brightics
- 모델링
- 브라이틱스
- 브라이틱스 서포터즈
- 직원 이직률
- 캐글
- 삼성SDS
- Brightics
Archives
- Today
- Total
데이터사이언스 기록기📚
[프로그래머스/Python] Level 2_메뉴 리뉴얼(2021 KAKAO BLIND RECRUITMENT) 본문
Coding Test/프로그래머스(Python)
[프로그래머스/Python] Level 2_메뉴 리뉴얼(2021 KAKAO BLIND RECRUITMENT)
syunze 2023. 6. 9. 15:32📌문제 유형
2021 KAKAO BLIND RECRUITMENT(구현)
📌문제
📌나의 문제풀이
- 점수 : 85.0
from itertools import combinations
from collections import Counter
def solution(orders, course):
menu = []
ans = []
#단품메뉴 추리기
for menus in orders:
for m in menus:
if m not in menu:
menu.append(m)
# 메뉴 개수 별
for num in course:
answer = []
list_combi = list(combinations(menu,num))
# combi 조합
for li_com in list_combi:
cnt, max_ = 0,0
# 손님이 주문한 조합(최대 20)
for order in orders:
tmp = set(order) - set(li_com)
if tmp | set(li_com) == set(order):
cnt += 1
# 가장 많이 나오는 개수 출력 - 조금 더 효율적인 방법이 있을 거 같은데
if cnt >= 2:
answer.append([li_com, cnt])
answer.sort(key = lambda x:x[1], reverse = True)
for menu_, max_ in answer:
if max_ == answer[0][1]:
ans.append(''.join(sorted(list(menu_))))
else:
break
ans.sort()
return ans
- 점수 : 100
- order과 list_combi 순서를 바꿈 -> 만들 수 있는 경우의 수가 줄어 시간초과 안 남!
from itertools import combinations
def solution(orders, course):
ans = []
for num in course:
answer = {}
for order in orders:
list_combi = list(combinations(sorted(order),num))
for li_com in list_combi:
cnt, max_ = 0,0
tmp = set(order) - set(li_com)
if tmp | set(li_com) == set(order):
if ''.join(li_com) in answer.keys():
answer[''.join(li_com)] += 1
else:
answer[''.join(li_com)] = 1
answer = sorted(answer.items(), key = lambda x:x[1], reverse = True)
for menu_, max_ in answer:
if max_ == answer[0][1] and max_ > 1:
ans.append(''.join(sorted(list(menu_))))
else:
break
ans.sort()
return ans
📌다른 사람의 문제풀이
- sort 대신 counter 이용
- 독립된 메뉴를 미리 찾지 않고, order 안에서 combination을 만들어서 검사
from itertools import combinations
from collections import Counter
def solution(orders, course):
ans = []
for num in course:
order_combi = []
for order in orders:
order_combi += combinations(sorted(order),num)
most_ordered = Counter(order_combi).most_common() # most_common : 개수 순으로 정렬하는 역할
ans += [k for k,v in most_ordered if v>1 and v == most_ordered[0][1]]
return [''.join(v) for v in sorted(ans)]
📌리뷰
- combination 사용할 때, 아무리 단어길이가 적더라도 더 적게 활용할 수 있는 방법 고려해보기
- order 먼저 -> combination(sort)로 검사 -> dict에 넣어서 개수 세기 -> 많은 순서대로 정렬하기
- 만약, combination에서 순서만 바뀐 단어가 count되고 있다면 sort 사용해서 순서 바뀐 단어 없애기
728x90
'Coding Test > 프로그래머스(Python)' 카테고리의 다른 글
[프로그래머스/Python] Level 2_수식 최대화 (2020 카카오 인턴십) (0) | 2023.09.06 |
---|---|
[프로그래머스/Python] Level 2_[3차]방금그곡 (2018 KAKAO BLIND RECRUITMENT) (0) | 2023.08.14 |
[프로그래머스/Python] Level 2_두 큐 합 같게 만들기(2022 KAKAO TECH INTERNSHIP) (0) | 2023.06.08 |
[프로그래머스/Python] Level 2_방문길이(Summer/Winter Coding(~2018)) (0) | 2023.05.17 |
[프로그래머스/Python] Level 2_[3차] 파일명 정렬(2018 KAKAO BLIND RECRUITMENT) (0) | 2023.05.17 |
Comments