문제
문제 답안
import java.util.*;
class Solution {
public int[] solution(int[][] score) {
int len = score.length;
int[] answer = new int[len];
double[] avg = new double[len];
double[] avg_copy = new double[len];
for(int i = 0; i < len; i++){
avg[i] = (double)(score[i][0]+score[i][1])/2;
avg_copy[i] = (double)(score[i][0]+score[i][1])/2;
}
Arrays.sort(avg_copy);
for(int i = 0; i < len; i++){
for(int j = 0; j < len; j++){
while(avg_copy[i] == avg[j]){
answer[j] = len-i;
//System.out.println(avg[j]);
break;
}
}
}
return answer;
}
}
답안 풀이
전에 풀었던 '특이한 정렬'이라는 문제랑 비슷하게 푼거같은데, 문제는 점수가 같으면 중복 등수를 주어진다는 것이였다.
처음에는 이중 for문을 돌리면서 while문 대신 if문을 써주었는데 이러면, 점수가 같으면 한 점수는 rank가 0으로 기록된다. 그래서 while문을 돌려주었더니 평균 점수가 소수점이 나오면 또 문제였다.(근데 이건 다른 사람들 풀이 보니까 굳이 2로 안나눠줘도 된다 왜냐면 점수를 구하는게 아닌 등수를 구하는거기때문에 점수의 합이 큰 순대로 하면 되기 때문이다)
참고할만한 코드
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()); //list를 내림차순으로 정렬
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;//indexOf는 중복되면 작은걸 가져옴
}
return answer;
}
}