[백준 2075] N번째 큰 수 (JAVA)

solser12·2021년 11월 30일
0

Algorithm

목록 보기
47/56

문제


https://www.acmicpc.net/problem/2075

풀이


최소 힙을 이용해 해결할 수 있습니다. 항상 최소 힙에 N개의 값이 들어있도록 유지하고 값이 하나 들어오면 Heapify를 통해 나온 값을 제거합니다.

코드


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());
        Heap heap = new Heap(N);

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            heap.addFirst(Integer.parseInt(st.nextToken()));
        }

        for (int i = 1; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                heap.add(Integer.parseInt(st.nextToken()));
            }
        }

        System.out.println(heap.getResult());
        br.close();
    }

    public static class Heap {
        int[] arr;
        int index;

        public Heap(int N) {
            arr = new int[N + 1];
            index = 0;
        }

        public void addFirst(int num) {
            arr[index] = num;
            endHeapify(index);
            index++;
        }

        public void add(int num) {
            if (arr[0] < num) {
                arr[0] = num;
                rootHeapify(0);
            }
        }

        public int getResult() {
            return arr[0];
        }

        public void endHeapify(int idx) {
            int temp = (idx - 1) >> 1;
            if (temp < 0) return;

            if (arr[temp] > arr[idx]) {
                swap(idx, temp);
                endHeapify(temp);
            }
        }

        public void rootHeapify(int idx) {
            int p = idx;
            int l = (idx << 1) + 1;
            int r = (idx << 1) + 2;

            if (l < index && arr[l] < arr[p]) {
                p = l;
            }
            if (r < index && arr[r] < arr[p]) {
                p = r;
            }

            if (p != idx) {
                swap(idx, p);
                rootHeapify(p);
            }
        }

        public void swap(int a, int b) {
            int temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;
        }
    }
}
profile
더 나은 방법을 생각하고 고민합니다.

0개의 댓글