[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv1] ์‹คํŒจ์œจ / Python

๐Ÿ“š Problem Solving/Programmers

https://programmers.co.kr/learn/courses/30/lessons/42889

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹คํŒจ์œจ

์‹คํŒจ์œจ ์Šˆํผ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž ์˜ค๋ ๋ฆฌ๋Š” ํฐ ๊ณ ๋ฏผ์— ๋น ์กŒ๋‹ค. ๊ทธ๋…€๊ฐ€ ๋งŒ๋“  ํ”„๋žœ์ฆˆ ์˜ค์ฒœ์„ฑ์ด ๋Œ€์„ฑ๊ณต์„ ๊ฑฐ๋’€์ง€๋งŒ, ์š”์ฆ˜ ์‹ ๊ทœ ์‚ฌ์šฉ์ž์˜ ์ˆ˜๊ฐ€ ๊ธ‰๊ฐํ•œ ๊ฒƒ์ด๋‹ค. ์›์ธ์€ ์‹ ๊ทœ ์‚ฌ์šฉ์ž์™€ ๊ธฐ์กด ์‚ฌ์šฉ์ž ์‚ฌ์ด์— ์Šค

programmers.co.kr

import sys

input = sys.stdin.readline

n = int(input())
stages = list(map(int, input().split()))


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


print(solution(n, stages))

 

ํ•ด์„ค

answer๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ •์˜ํ•ด์ค€๋‹ค. ์Šคํ…Œ์ด์ง€๋ณ„๋กœ ์‹คํŒจ์œจ์„ ์ €์žฅํ•œ๋‹ค.

 

๊ฐ ์Šคํ…Œ์ด์ง€๊นŒ์ง€๋งŒ ํ•ด๊ฒฐํ•œ ์ธ์› ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๋ฉด ๋‹ค์Œ ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ์ธ์›์ด ๋œ๋‹ค. -> length

์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ์ธ์›์ด ์—†์„ ๋•Œ(length==0)๋Š” ์‹คํŒจ์œจ์„ 0์ด๋ฏ€๋กœ answer[stage]์— 0์„ ์ €์žฅํ•œ๋‹ค.

 

์‹คํŒจ์œจ์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.