[programmers] 실패율_sorting

Date:

블럭 이동하기

link

내 풀이

  • 방식 : sorting
  • Sorted : O(nlogn)

📰code

def solution(N, stages):

    sorted_stages = sorted(stages, reverse = True)
    sorted_stages.append(0)

    fail_rate = {i+1:0 for i in range(N)}
    challenge_n = 0

    for i in range(len(stages)):
        if sorted_stages[i]>N : continue
        else :
            challenge_n+=1
            if sorted_stages[i] > sorted_stages[i+1]:
                fail_rate[sorted_stages[i]] = -(challenge_n)/(i+1)
                challenge_n = 0

    sorted_fail_rate = sorted(fail_rate.items(), key = lambda x:(x[1],x[0]))

    answer = [i for i, _ in sorted_fail_rate] 

    return answer

다른 사람 풀이

📰code

def solution(N, stages):
    answer = []
    fail = []
    info = [0] * (N + 2)
    for stage in stages:
        info[stage] += 1
    for i in range(N): 
        be = sum(info[(i + 1):])
        yet = info[i + 1]
        if be == 0:
            fail.append((str(i + 1), 0))
        else:
            fail.append((str(i + 1), yet / be))
    for item in sorted(fail, key=lambda x: x[1], reverse=True):
        answer.append(int(item[0]))
    return answer

추가 수정

📰code

def solution(N, stages):
    
    count = [0]*(N+1)
    for i in stages:
        count[i-1] += 1
    
    fail_rate = []
    for i in range(N):
        if count[i]==0: fail_rate.append(0)
        else : fail_rate.append(-(count[i])/(sum(count[i:])))
        
    answer = sorted(range(1,N+1),key = lambda x : fail_rate[x-1])
    return answer
💡 수정 필요한 내용은 댓글이나 메일로 알려주시면 감사하겠습니다!💡 

댓글