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;
}
}
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;
}
}
댓글
댓글 쓰기