[プログラマーSweetチャレンジ]#85006週ランキングボクサー



質問する


パラメータはボクサーの体重とボクサーの戦績のhead 2 headです.ボクサーの番号を以下の順序で並べ、解題関数を完了して戻ります.
  • 総勝率の高いボクサーの番号が前にあります.他のボクサーと対決したことのないボクサーの勝率は0%だった.
  • の勝率が同じボクサーの番号の中で、体重が自分より重いボクサーが勝った回数が多いボクサーの番号を前に進みます.
  • 自分より重いボクサーに勝った回数が同じボクサーの番号の中で、重いボクサーの番号が前に進む.
  • 自分の体重が同じ福書番号の中で、小さいサイズが前に進みます.
  • I/O例


    weightshead2headresult[50,82,75,120]["NLWL","WNLL","LWNW","WWLN"][3,4,1,2][145,92,86]["NLW","WNL","LWN"][2,3,1][60,70,60]["NNN","NNN","NNN"][2,1,3]

    に答える


    この問題は、優先キューを使用して、対応する条件の複素シーケンスを順番にソートして答えを出力することができる.
    import java.util.PriorityQueue;
    
    class Solution {
        public int[] solution(int[] weights, String[] head2head) {
            int[] answer = new int[weights.length];
            PriorityQueue<Boxer> pq = new PriorityQueue<>();
            
            for(int i=0; i<weights.length; i++) {
                String res = head2head[i];
                double percent = 0;
                int total = 0;
                int win = 0;
                int cnt = 0;
                int weight = weights[i];
                
                for(int j=0; j<res.length(); j++) {
                    char c = res.charAt(j);
                    if(c!='N') {
                        total++;
                        if(c=='W') {
                            win++;
                            if(weight<weights[j])
                                cnt++;
                        }
                    }
                }
                if(total!=0)
                    percent = (double)win/total;
         
                pq.add(new Boxer(i+1, percent, cnt, weight));
            }
            
            int idx = 0;
            while(!pq.isEmpty()) {
                answer[idx++] = pq.poll().idx;
            }
            
            
            return answer;
        }
        
        public class Boxer implements Comparable<Boxer>{
            int idx;
            double percent;
            int cnt;
            int weight;
            
            public Boxer(int idx, double percent, int cnt, int weight) {
                this.idx = idx;
                this.percent = percent;
                this.cnt = cnt;
                this.weight = weight;
            }
            
            public int compareTo(Boxer b) {
                if(this.percent==b.percent) {
                    if(this.cnt == b.cnt) {
                        if(this.weight==b.weight)
                            return this.idx > b.idx ? 1 : -1;
                        
                        return this.weight < b.weight ? 1 : -1;
                    }
                    
                    return this.cnt < b.cnt ? 1 : -1;
                }
                
                return this.percent < b.percent ? 1 : -1;
            }
        }
    }