ํ’€์ด

์ผ๋‹จ, 200,000์ด๋ผ์„œ n^2์€ ์•ˆ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ•œ๋ฒˆ์— ๊ฐ€์•ผํ•˜๋Š”๋ฐ, ์ด ๋•Œ, ์ž˜ ์‚ดํŽด๋ณด๋ฉด, ํ•ด๋‹น ์Šคํ…Œ์ด์ง€์—์„œ์˜ ์‹คํŒจ์œจ์€ stage์ž”์กด ์ˆ˜/stage ํ†ต๊ณผ์ˆ˜๋กœ ์ •์˜๋œ๋‹ค. ํ†ต๊ณผ์ˆ˜์˜ ๊ฒฝ์šฐ 4์Šคํ…Œ์ด์ง€์— ์žˆ๋Š” ๊ฒฝ์šฐ ์ด์ „ 1, 2, 3์Šคํ…Œ์ด์ง€์— ์žˆ์„ ๋•Œ ๋ชจ๋‘ ์นด์šดํŠธ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ถ€๋ถ„์„ ์ž˜ ์‚ดํŽด์•ผ ํ•œ๋‹ค.

์ฆ‰, ํ˜„์žฌ ์Šคํ…Œ์ด์ง€์— ์žˆ๋Š” ์ˆ˜ / ํ˜„์žฌ ์Šคํ…Œ์ด์ง€๋ณด๋‹ค ํฐ ์Šคํ…Œ์ด์ง€์— ์žˆ๋Š” ์‚ฌ๋žŒ์ˆ˜ ๊ฐ€ ์‹คํŒจ์œจ์ด๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด, ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ํฐ ์Šคํ…Œ์ด์ง€ ์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ •๋ ฌ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ํฌ์ธํ„ฐ๋ฅผ ์ญ‰ ์ด๋™ํ•˜๋ฉด์„œ ํ•œ๋ฒˆ์— ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋งž๋‹ค. 0์ธ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ์ž˜ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

Code

def solution(N, stages):
    success = dict([[x, 0] for x in range(1,N+2)])
    tries = dict([[x, 0] for x in range(1,N+2)])
    
    stages.sort()
    i = 0
    stage_num = 1    
    while i < len(stages):
        if stages[i] == stage_num:
            success[stage_num] += 1
            i += 1
        else:
            tries[stage_num] = len(stages)-i
            stage_num += 1
    
    fail_ratio = []
    for num in range(1, N+1):
        if success[num]+tries[num] == 0:
            fail_ratio.append([num, 0])
        else:
            fail_ratio.append([num, success[num]/(success[num]+tries[num])])
    
    fail_ratio = sorted(fail_ratio, key=lambda x: (x[1], -x[0]), reverse=True)
    
    return [x[0] for x in fail_ratio]

Reference