[Algorithm] 징검다리 건너기, 메뉴 리뉴얼

리쫑·2023년 1월 27일
0

Algorithm

목록 보기
7/16

징검다리 건너기

🤡징검다리 건너기 (Lv.3)

🤑풀이

import pandas as pd

def solution(stones, k) :
    return int(min(list(pd.Series(stones).rolling(k).max())[k-1:]))

👩‍🏫 아이디어

  • k rolling window & max operation
  • min for above series data
  • pandas가 지원되지 않는 시험 사이트도 있으므로, 직접 구현하는 것도 해봐야 겠다.

메뉴 리뉴얼

🤡메뉴 리뉴얼 (Lv.2)

🤑풀이

from itertools import  combinations


def solution(orders, course):
    info = {}
    for set_menu in orders :
        for n in course :
            # course의 개수만큼 메뉴 조합 생성
            if len(set_menu) < n :
                break    
            new_combi = combinations(set_menu, n)
            for new_set in new_combi :
                new_set = ''.join(sorted(new_set))
                info[new_set] = info.get(new_set, 0) + 1
    
    # 주문 정보를 담은 dictionary
    # {"메뉴 개수" : {max : 주문 수, lst : max 주문수가 일치하는 세트들의 모음 } 
    order = {}
    for i in course :
        order[i] = {'max' : 0, 'lst' : []}
        
    for cor in info :
        menu_cnt, order_cnt = len(cor), info[cor]
        if order[menu_cnt]['max'] < order_cnt :
            order[menu_cnt]['max'] = order_cnt
            order[menu_cnt]['lst'] = [cor]
        elif order[menu_cnt]['max'] == order_cnt :
            order[menu_cnt]['lst'].append(cor)
        
    answer_lst = []
    for i in order :
        if order[i]['max'] > 1 :
            answer_lst.extend(order[i]['lst'])

    return sorted(answer_lst)

👩‍🏫 아이디어

  • itertools의 combination을 사용하고, dictionary 형태로 객체를 만들어서 풀이.
  • order라는 dictionary 객체를 만들어 course(메뉴 개수)별로 max(최대 주문 수), lst(max번 주문한 세트 메뉴들) 정보를 담아서 풀이.

Counter.most_common

  • 다른 분들의 풀이를 보니 collection 라이브러리의 Counter.most_common를 사용하셨다. 다음엔 Counter.most_common를 사용해서 코드를 깔끔하게 작성해봐야 겠다.
profile
AI, Data Scientist 취업 준비생 입니다. 공부한 내용을 포스팅하고자 합니다. 방문해주셔서 감사합니다

0개의 댓글