코딩 테스트/정렬
실패율
hazel_
2021. 2. 15. 19:33
p.361
방법 1
def solution(N, stages):
stop = [0] * (max(stages)+1)
stop_ratio = []
people = len(stages)
result=[]
for i in range(people):
stop[stages[i]] += 1
for i in range(1, N+1):
if people==0:
stop_ratio.append((i,0))
continue
ratio=stop[i]/people
people -= stop[i]
stop_ratio.append((i, ratio))
stop_ratio.sort(key=lambda x: (-x[1]))
for i in range(len(stop_ratio)):
result.append(stop_ratio[i][0])
return result
* stop_ratio를 구하는 과정에서 people이 0이 됐을 때를 continue로 넘어가주지 않으면 런타임 에러 발생
방법 2
def solution(N, stages):
answer=[]
people=len(stages)
for i in range(1, N+1):
count = stages.count(i)
if people==0:
fail=0
else:
fail=count/people
answer.append((i,fail))
people-=count
answer=sorted(answer,key=lambda x:(-x[1],x[0]))
answer=[i[0] for i in answer]
return answer