18870. 좌표압축

jp·2023년 4월 3일
0

baekjoon

목록 보기
15/15

문제

코드 & 풀이

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

loc = sorted(set(xs))
for i in xs:
    print(loc.index(i), end=" ")
  • 입력 범위가 x가 +- 10억이라서 index 탐색 시 최악의 경우 20억개를 일일히 탐색해야 함
import sys
input = sys.stdin.readline

n = int(input())
xs = list(map(int, input().split()))
loc_dict = {}

loc = sorted(set(xs))
for l in range(len(loc)):
    loc_dict[loc[l]] = l

for i in xs:
    print(loc_dict[i], end=" ")
  • 미리 dict에 저장해놓고 꺼내쓰면 index를 찾으려고 리스트 탐색을 하지 않아도 됨
profile
응애 개발자지망생이 알고리즘에 고통받는 중

0개의 댓글