Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이연주 : boj 18870 좌표압축 #243

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

이연주 : boj 18870 좌표압축 #243

wants to merge 3 commits into from

Conversation

Joylish
Copy link
Contributor

@Joylish Joylish commented May 3, 2021

🅰 설계

  • set : 중복된 원소를 없애기
  • list : set 자료구조를 인덱스로 원소에 접근하기 위해 List 자료구조로 변경
  • arr : 입력된 순서대로 값이 저장된 배열
static Set<Integer> set = new HashSet<>();
static List<Integer> list;
static int[] arr;
for (int i = 0; i < n; i++) {
    int num = Integer.parseInt(st.nextToken());
    arr[i] = num;
    set.add(num);
}
list = new ArrayList<>(set);
Collections.sort(list);
int len = list.size();
  • map : 정렬된 list에 대한 값을 key로 갖고 그에 대한 인덱스를 value로 갖는다.
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len; i++) {
    map.putIfAbsent(list.get(i), i);
}
for(int num : arr){
    sb.append(map.get(num)).append(" ");
}

✅ 후기

  • 그 전까지 자료구조 map과 set을 잘 다루지 않았지만 이 문제를 풀면서 이 두가지 자료구조를 쓸 수 있어 좋았습니다.

@Joylish Joylish added the BOJ Baekjoon label May 3, 2021
@Joylish Joylish self-assigned this May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BOJ Baekjoon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant