2019 카카오 신입 공채 1차 - 실패율

문제풀기. : https://www.welcomekakao.com/learn/courses/30/lessons/42889
2번 문제 실패율
각 스테이지 마다 실패율을 계산하고
실패율이 높은 스테이지 순 ( 내림차순) 정렬
실패율이 같으면 스테이지 오름차순 정렬

import java.util.*;
class Solution {
    static class StageFailInfo implements Comparable<StageFailInfo> {
        double rate;
        int stage;
        public StageFailInfo(int stage, double rate) {
            this.stage = stage;
            this.rate = rate;
        }
        @Override
        public int compareTo(StageFailInfo o) {
            if (this.rate > o.rate) {
                return -1;
            } else if (this.rate < o.rate) {
                return 1;
            } else if (this.rate == o.rate) {
                if (this.stage < o.stage) {
                    return -1;
                } else if (this.stage > o.stage) {
                    return 1;
                }
            }
            return 0;
        }
    }
       public static int[] solution(int N, int[] stages) {
        int[] answer = new int[N];
        List<StageFailInfo> pq = new ArrayList<>();
        double reachCnt = stages.length;
        int stage = 0;
        while(stage < N){
            stage++;
            double notClear = 0;
            for ( int i = 0; i < stages.length; i++) {
                if(stages[i] == stage)
                    notClear++;
            }
            pq.add(new StageFailInfo(stage, notClear/reachCnt));
            reachCnt -= notClear;
        }
        Collections.sort(pq);
        for (int i = 0; i < pq.size(); i++) {
            answer[i] = pq.get(i).stage;
        }
        return answer;
    }
}

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About idempotent

About G1 GC

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

sneak peek jitpack

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

DDD(Domain Driven Design) - Aggregate (어그리게잇)

Strategy Pattern In Spring (feat. JPA)