프로그래머스 실패율

sunghun Jung·2022년 5월 21일
1

codingtest

목록 보기
1/1

매개 값으로 넘어오는 전체 스테이지 수, 유저가 도전중인 스테이지가 담긴 배열을 이용해 실패율이 높은 스테이지부터 내림차순으로 정렬하는 문제

이 문제를 풀면서 map, 혹은 배열을 이용해 특정 값을 기준으로 값을 누적시키는 방법을 알아보았고 sort()나 Comparable 등의 정렬 방법도 사용해본적은 없었는데 실제로 사용하면서 꽤 재밌었다.

새롭게 알 게 된 것은 정수 / 정수 의 나누기는 분모가 0일 때 ArithmeticException이 발생하지만 분자 혹은 분모 중 하나가 실수형일 경우 ArithmeticException이 발생하지 않는다는 점!

  • 여러가지 방법으로 풀어본 문제
    • 첫 번째 풀이 (스테이지 별 사용자 누적 방법 - 배열 / 스테이지 별 실패율 - Map , 정렬 방법 - Collections.sort)
      class Solution {
          public int[] solution(int N, int... stages) {
              int[] challengerByStage = new int[N + 2];
              for (int stage : stages) {
                  challengerByStage[stage] += 1;
              }
      
              int remainingPlayers = stages.length;
              Map<Integer, Double> failureByStage = new HashMap<>();
              for (int stage = 1; stage <= N; stage++) {
                  double failRate = 0.0;
      
                  if(challengerByStage[stage]!=0){
                      failRate = (double) challengerByStage[stage] / remainingPlayers;
                      remainingPlayers -= challengerByStage[stage];
                  }
                  failureByStage.put(stage, failRate);
              }
      
              List<Integer> stageSet = new ArrayList<>(failureByStage.keySet());
              Collections.sort(stageSet, (o1, o2) -> failureByStage.get(o2).compareTo(failureByStage.get(o1)));
      
              int answer[] = new int[stageSet.size()];
              int index = 0;
              for (int i : stageSet) {
                  answer[index++] = i;
              }
      
              return answer;
          }
      }
    • 두 번째 풀이 (스테이지 별 사용자 누적 방법 - Map.getOrDefault 사용
      public int[] solution(int N, int... stages) {
              Map<Integer, Integer> challengerByStage = new HashMap<>();
              for (int i : stages) {
                  challengerByStage.put(i, challengerByStage.getOrDefault(i, 0) + 1);
              }
      
              int remainingPlayers = stages.length;
              Map<Integer, Double> failureByStage = new HashMap<>();
              for (int stage = 1; stage <= N; stage++) {
                  double failRate = 0.0;
      
                  if(challengerByStage.containsKey(stage)){
                      failRate = (double) challengerByStage.get(stage) / remainingPlayers;
                      remainingPlayers -= challengerByStage.get(stage);
                  }
                  failureByStage.put(stage, failRate);
              }
      
              List<Integer> stageSet = new ArrayList<>(failureByStage.keySet());
              Collections.sort(stageSet, (o1, o2) -> failureByStage.get(o2).compareTo(failureByStage.get(o1)));
      
              // 4. 배열에 옮겨담음
              int answer[] = new int[stageSet.size()];
              int index = 0;
              for (int i : stageSet) {
                  answer[index++] = i;
              }
      
              return answer;
          }
    • 세 번째 풀이 (Comparable을 구현한 클래스로 정렬 기준 제공)
      public int[] solution(int N, int... stages) {
      
              int[] playersByStage = new int[N+2];
              for (int i : stages) {
                  playersByStage[i]++;
              }
      
              int remainingPlayers = stages.length;
              List<Stage> stageList = new ArrayList<>();
              for (int stage = 1; stage <= N; stage++){
                  double failure = (double) playersByStage[stage] / remainingPlayers;
                  remainingPlayers -= playersByStage[stage];
      
                  stageList.add(new Stage(stage, failure));
              }
      
              Collections.sort(stageList, Collections.reverseOrder());
      
              int[] answer = new int[N];
              for(int i=0; i<N; i++){
                  answer[i] = stageList.get(i).stage;
              }
      
              return answer;
          }
      
          class Stage implements Comparable<Stage> {
              public int stage;
              public double failure;
      
              public Stage(int stage, double failure) {
                  this.stage = stage;
                  this.failure = failure;
              }
      
              @Override
              public int compareTo(Stage o) {
                  return (failure < o.failure ? -1 : (failure > o.failure ? 1 : 0));
              }
          }

0개의 댓글