글 작성자: Doublsb

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

 

풀이

- 강한 복서 순서대로 정렬하는 문제

- 복서의 번호, 전적, 몸무게 값이 입력되어 들어옴

- 승률이 높은 순서, 같다면 체급이 높은 쪽을 이긴 횟수, 같다면 체급이 높은 쪽, 같다면 번호가 빠른 쪽 순서로 정렬해야 함

 

- 하하 Linq 쓸거야 하하

 

C#

using System;
using System.Linq;

public class Solution {
    
    public static Boxer[] Boxers;
    
    public int[] solution(int[] weights, string[] head2head) {
        
        Boxers = new Boxer[weights.Length];
        for(int i=0; i<weights.Length; i++){
            Boxers[i] = new Boxer(i, weights[i], head2head[i]);
        }
        
        foreach(var e in Boxers) e.Calculate_Rate();

        return Boxers.OrderByDescending(e => e.Rate)
                     .ThenByDescending(e => e.HeavierWinCount)
                     .ThenByDescending(e => e.Weight)
                     .ThenBy(e => e.Number)
                     .Select(e => e.Number + 1)
                     .ToArray();
    }
    
    public class Boxer {
        public int Number;
        public int Weight;
        
        public float Rate;
        public int HeavierWinCount;
        
        private string head2head;
        
        public Boxer(int Number, int Weight, string head2head){
            this.Number = Number;
            this.Weight = Weight;
            this.head2head = head2head;
        }
        
        public void Calculate_Rate(){
            
            float winCount = 0;
            float loseCount = 0;
            
            for(int i=0; i<Boxers.Length; i++){
                switch(head2head[i]){
                    case 'W':
                        if(Boxers[i].Weight > Weight) HeavierWinCount++;
                        winCount++;
                        break;
                    case 'L':
                        loseCount++;
                        break;
                }
            }
            
            if(winCount == 0 || (winCount == 0 && loseCount == 0)) Rate = 0;
            else Rate = winCount / (winCount + loseCount);
        }
    }
}

 

결과

100.0 / 100.0

 

피드백

즉사 치트를 써버린 기분. 라이브러리 개발자분들께 깊은 감사를 드립니다 (...)

반응형