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 좌표압축 #245

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

Conversation

hoonti06
Copy link

@hoonti06 hoonti06 commented May 4, 2021

🅰 설계

  • <원소 값, 순서>를 저장하는 map (결과를 출력할 때 O(1)에 access 가능)

    static HashMap<Integer, Integer> cntMap;
  • 정렬되어 있는 배열에서 upper-bound를 이용하여 중복되는 원소를 건너뛰는 방식으로 진행하면서 순서(cnt)를 계산했습니다. 그 순서(value)를 원소값(key)과 함께 cntMap에 저장하였습니다.

    int lower = 0;
    int cnt = 0;
    while (lower < N) {
      int key = sorted[lower];
      int upper = upperBound(key);
    
      cntMap.put(key, cnt);
      lower = upper;
      cnt++;
    }
  • upper-bound를 직접 구현했습니다.

    static int upperBound(int key) {
        int left = 0;
        int right = N-1;
        int res = N; // default 값 중요
        while (left <= right) {
            int mid = (left + right) / 2;
            if (key < sorted[mid]) {
                right = mid - 1;
                res = mid;
            } else {
                left = mid + 1;
            }
        }
        return res;
    }

✅ 후기

  • upperbound를 직접 구현해봐서 좋았습니다.
  • 시간이 많이 느린 것 같아서 다른 풀이 방법과 비교해봐야 할 것 같습니다.

@hoonti06 hoonti06 self-assigned this May 4, 2021
@hoonti06 hoonti06 added BOJ Baekjoon 이분탐색 binary search labels May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BOJ Baekjoon 이분탐색 binary search
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant