2910 : 빈도 정렬

서희찬·2022년 1월 21일
0

백준

목록 보기
96/105

문제

코드

#빈도, 들어온 순서 
n,c = map(int,input().split())
arr = list(map(int,input().split()))

bindo = {} #빈도, 처음 등장위치 

for i in range(n):
    if arr[i] in bindo:
        bindo[arr[i]][0]+=1 
    else :
        bindo[arr[i]]=[1,i] #빈도,위치 

sortBindo = sorted(bindo.items(),key = lambda x : (-x[1][0],x[1][1]))

result = []
for i in sortBindo:
    for j in range(i[1][0]):
        result.append(str(i[0]))
print(" ".join(result))

해설

딕셔너리를 활용하는 문제이다.
key로 입력된 수를 잡고
value를 [빈도,위치]를 넣는 방법이다.
그렇게 sorted를 써서 원하는대로 정렬을 진행 한 후
result에 정렬된 key를 빈도 만큼 곱해주면서 수열을 만들어주고 join으로 출력을 진행해주면 된다.

profile
Carnegie Mellon University Robotics Institute | Research Associate | Developing For Our Lives, 세상에 기여하는 삶을 살고자 개발하고 있습니다

0개의 댓글

관련 채용 정보