개발자-H 입니다.

프로그래머스 - 복서 정렬 하기 (6주차) 본문

Algorithm/문제 풀이

프로그래머스 - 복서 정렬 하기 (6주차)

개발자-H 2021. 9. 12. 17:01

https://programmers.co.kr/learn/courses/30/lessons/85002

 

코딩테스트 연습 - 6주차

복서 선수들의 몸무게 weights와, 복서 선수들의 전적을 나타내는 head2head가 매개변수로 주어집니다. 복서 선수들의 번호를 다음과 같은 순서로 정렬한 후 return 하도록 solution 함수를 완성해주세요

programmers.co.kr

 

  • 주어진 조건에 따라 복서에게 우선 순위를 부여하여 정렬하면 된다.
  • 우선 순위 정렬는 PriorityQueue를 사용하여 정렬하였다.

 

import java.util.*;

class Solution {
        public int[] solution(int[] weights, String[] head2head) {
            List<Boxer> boxers = new ArrayList<>();

            for (int i = 0; i < weights.length; i++) {
                boxers.add(new Boxer(weights[i], i + 1));
            }

            for (int i = 0; i < head2head.length; i++) {
                char[] histories = head2head[i].toCharArray();
                Boxer boxer = boxers.get(i);
                for (int j = 0; j < histories.length; j++) {
                    if (histories[j] == 'N') continue;
                    boxer.addHistory(histories[j], boxers.get(j));
                }
            }

            PriorityQueue<Boxer> queue = new PriorityQueue<>();
            for (int i = 0; i < boxers.size(); i++) {
                queue.offer(boxers.get(i));
            }


            int[] answer = new int[boxers.size()];
            int i = 0;
            while (!queue.isEmpty()) {
                answer[i] = queue.poll().id;
                i++;
            }

            return answer;
        }

        public class Boxer implements Comparable<Boxer> {
            public int winCount; // 이긴 경기 수 
            public int loseCount; // 진 경기 수
            public int playCount; // 경기 횟수
            public double winRate; // 승률
            public int winFromHigherWeight; // 몸무게가 많이 나간 사람에게 이긴 수
            public int weight; // 자기 몸무게 
            public int id; // 자기 번호

            public Boxer(int weight, int number) {
                this.weight = weight;
                this.id = number;
            }

            public void addHistory(char history, Boxer boxer) {
                playCount += 1;
                if (history == 'W') {
                    if (boxer.weight > this.weight) {
                        winFromHigherWeight += 1;
                    }
                    winCount += 1;

                } else if (history == 'L') {
                    loseCount += 1;
                }

                winRate = ((double) winCount / (winCount + loseCount)) * 100.0;
            }

            @Override
            public int compareTo(Boxer other) {
                if (this.winRate != other.winRate) return other.winRate - this.winRate > 0 ? 1 : -1;
                if (this.winFromHigherWeight != other.winFromHigherWeight) return other.winFromHigherWeight - this.winFromHigherWeight;
                if (this.weight != other.weight) return other.weight - this.weight;

                return this.id - other.id;
            }
        }
    }

'Algorithm > 문제 풀이' 카테고리의 다른 글

BOJ - 부분수열의 합  (0) 2021.09.18
프로그래머스 - 위클리 챌린지 7주차  (0) 2021.09.16
BOJ - 프린터 큐  (0) 2021.09.09
BOJ - 쿼드트리  (0) 2021.09.08
BOJ - 나는야 포켓몬 마스터 이다솜  (0) 2021.09.08
Comments