2019 KAKAO BLIND RECRUITMENT失敗率(lv 1)


解答コード1

def solution(N, stages):
    answer = []
    arrive = [0 for i in range(N + 2)]
    current = [0 for i in range(N + 2)]

    for s in stages:
        for j in range(1, s + 1):
            arrive[j] += 1
        current[s] += 1

    print(arrive)
    print(current)
    temp = []
    for i in range(1, N + 1):
        if arrive[i] == 0:
            temp.append([0, i])
        else:
            temp.append([current[i] / arrive[i], i])

    print(temp)
    temp.sort(reverse=True, key=lambda x: x[0])
    for i in range(N):
        answer.append(temp[i][1])

    return answer

解答コード2

def solution(N, stages):
    answer = {}
    players = len(stages)
    
    for i in range(1, N + 1):
        if players != 0:
            current = stages.count(i)
            answer[i] = current / players
            players -= current
        else:
            answer[i] = 0
            
    return sorted(answer, reverse = True, key = lambda x : answer[x])

学識


1番目のコードに比べて、2番目のコードの実行速度はずっと速い.
時間の複雑さを高める悩みが欠けている.
ダンディーダンスを聞きながらコードすればいい