[백준 BOJ_18870] 좌표 압축 Python 풀이

출처: 백준 온라인 저지

문제

image image

풀이

입력받은 list를 중복제거 해준 뒤 정렬해주어 다시 sorted_X에 저장해줍니다. 정렬된 list를 바탕으로 해당 숫자를 key로, index를 value로 가지는 dictionary를 만들어줍니다.

그 후에는 입력받은 list를 다시 한번 돌아주며 해당하는 index를 new_X에 넣어준 뒤 출력해주었습니다.

코드

import sys

input = sys.stdin.readline

N = int(input())

X = list(map(int, input().split()))
sorted_X = sorted(set(X))
new_X = []

idx = {num:i for i, num in enumerate(sorted_X)}
for num in X:
  new_X.append(idx[num])

print(*new_X, sep=" ")

Leave a comment