목차

순위 검색

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호72412
문제명순위 검색
레벨Level 2
분류

구현, 이진검색

시간복잡도O((n+q)logn)
인풋사이즈n<=50000, q<=100000
사용한 언어Python
해결날짜2021/01/26
출처

ps:problems:programmers:2021_kakao_blind_recruitment

풀이

코드

"""Solution code for "Programmers 72412. 순위 검색".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/72412
- Solution link: http://www.teferi.net/ps/problems/programmers/72412
"""

import bisect
import collections


def solution(info, query):
    scores_by_type = collections.defaultdict(list)
    for resume in info:
        *types, score = resume.split()
        scores_by_type[tuple(types)].append(int(score))
    for scores in scores_by_type.values():
        scores.sort()

    answer = []
    for q in query:
        q_list = q.split()
        type_filter = q_list[::2]
        score_filter = int(q_list[-1])
        count = sum(
            len(scores) - bisect.bisect_left(scores, score_filter)
            for types, scores in scores_by_type.items()
            if all(f in ['-', t] for f, t in zip(type_filter, types)))
        answer.append(count)

    return answer