practivceAlgorithm/programmers

[Programmers][Python] KAKAO 2019 실패율

findTheValue 2021. 10. 28. 20:06

정렬 후 upperbound, lowerbound로

분모는 전체 - lowerbound

분자는 upperbound - lowerbound

로 실패율 연산 후 최대힙으로 정렬

 

from bisect import bisect_left, bisect_right
from heapq import heappush, heappop

def solution(N, stages):
    answer = []
    stages = sorted(stages)
    cnts = []
    for i in range(1, N + 1):
        left = bisect_left(stages, i)
        right = bisect_right(stages, i)
        total = len(stages) - left
        failure_rate = (right - left) / total if total else 0
        heappush(cnts, (-failure_rate, i))
    while cnts:
        answer.append(heappop(cnts)[1])
    return answer