[프로그래머스]순위 검색

Lee·2021년 1월 27일
0

https://programmers.co.kr/learn/courses/30/lessons/72412

from collections import defaultdict
from bisect import bisect_left, insort
def solution(info, query):
    answer = []
    graph = defaultdict(list)
    
    # 점수를 제외하면 개발언어(3), 직군(2), 경력(2), 소울푸드(2), 총 24 종류의 경우가 발생한다.
    # 24종류를 사전형의 key 값으로 설정하고, 점수를 insort 함수를 통해 정렬되게 넣는다.
    for person in map(lambda x:x.split(), info):
        insort(graph[tuple(person[:-1])], int(person[-1]))
        
    # query에서 먼저 24 종류 중 하나를 확인하고, 각각의 values는 정렬된 상태이므로
    # bisect_left(이분탐색) 모듈을 활용하여 만족하는 점수 개수를 얻는다.
    for require in map(lambda x:x.replace('and', '').split(), query):
        stack = list(graph.keys())
        
        for idx, what in enumerate(require[:-1]):
            if what != '-':
                stack = [temp for temp in stack if temp[idx] == what]
                
        answer.append(sum([len(graph[x]) - bisect_left(graph[x], int(require[-1])) for x in stack]))
    return answer
profile
초보 개발자입니다

0개의 댓글