[プログラマーSweetチャレンジ]#85006週ランキングボクサー
14490 ワード
質問する
パラメータはボクサーの体重とボクサーの戦績のhead 2 headです.ボクサーの番号を以下の順序で並べ、解題関数を完了して戻ります.
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;
}
}
}
Reference
この問題について([プログラマーSweetチャレンジ]#85006週ランキングボクサー), 我々は、より多くの情報をここで見つけました https://velog.io/@pss407/프로그래머스-위클리-챌린지85002-6주차복서-정렬하기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol