class Solution {
public int[] solution(int[][] score) {
int[] answer = new int[score.length];
double[] avg = new double[score.length];
for(int i=0; i<score.length; i++) {
avg[i] = (score[i][0]+score[i][1]) /(double)2;
}
for(int i=0; i<avg.length; i++) {
for(int j=0; j<avg.length; j++) {
if(avg[i] < avg[j] && i != j) {
answer[i]++;
}
}
answer[i]++;
}
return answer;
}
}
다른 풀이를 찾다가 반복문을 한 번 덜 쓰고 로직도 깔끔한 것 같고 배울점이 있는 코드를 발견해서 공부한다.
import java.util.*;
class Solution {
public int[] solution(int[][] score) {
List<Integer> scoreList = new ArrayList<>();
for(int[] t : score){
scoreList.add(t[0] + t[1]);
}
scoreList.sort(Comparator.reverseOrder());
int[] answer = new int[score.length];
for(int i=0; i<score.length; i++){
answer[i] = scoreList.indexOf(score[i][0] + score[i][1])+1;
}
return answer;
}
}