Til


title: 2026-01-28 author: 강병호 (이름) date: 2026-01-28 (날짜) category: TIL/강병호/2026/01 (파일 경로 : TIL/{이름}/{연}/{월}) layout: post (자유) —

/*
 * input
 * N : 1 ~ 500
 * stages : 최대 200_000
 * stage[i] : 스테이지 번호, 도전 중인 사람 수
 */

// 스테이지에 도달 + 클리어하지 못한 플레이어 수 => N번째 갔지만 N+1에는 없는 플레이어 수

/*
    output
    실패율 높은 스테이지를 내림차순으로 담기 => PQ <스테이지 번호, 실패 율>
*/

import java.util.*;

class Solution {
    static class Stage implements Comparable<Stage> {
        int id;
        double rate;

        Stage(int id, double rate) {
            this.id = id;
            this.rate = rate;
        }

        @Override
        public int compareTo(Stage s) {
            // 실패율 더 높은 스테이지가 우선순위가 더 높다.
            if (this.rate < s.rate) return 1;
            else if (this.rate > s.rate) return -1;
            else  {
                if (this.id > s.id) return 1;
                else if (this.id == s.id) return 0;
                else return -1;
            }
        }
    }

    static PriorityQueue<Stage> pq = new PriorityQueue<>();

    static int[] data = new int[502];

    public int[] solution(int N, int[] stages) {

        int length = stages.length; // 사람 수

        for (int i = 0; i < length; i++) {
            int p = stages[i];

            data[p]++;
        }

        for (int i = 1; i <= N; i++) {
            if (length == 0) {
                pq.add(new Stageß(i, 0.0));
            } else {
                double rate = (double) data[i] / (double) length;
                pq.add(new Stage(i, rate));
                length -= data[i];a
            }
        }

        int[] answer = new int[N];
        for (int i = 0; i < N; i++) {
            answer[i] = pq.poll().id;
        }

        return answer;
    }
}

results matching ""

    No results matching ""