Java/Programmers

[Lv.0]등수 매기기

GLOWWW 2023. 1. 17. 22:21

처음 설계한것은 아래의 사진과 같다.

그러나 sorting에서 문제가 생겼다. 같은 등수일 경우가 있는데 이것을 해결하는 방법이 떠오르지 않았다.

결국 다른방법으로 풀었는데..

우선 answer배열을 Arrays.fill을 사용하여 1로 채워넣었다.

그 후 2중 반복문을 수행하여 스코어를 비교하였고, 더 크면 그때마다 ++ 해주었다.

import java.util.Arrays;

public class 등수매기기 {
    public static int[] solution(int[][] score) {
        int[] answer = new int[score.length];
        Arrays.fill(answer, 1);
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score.length; j++) {
                if (score[i][0] + score[i][1] > score[j][0] + score[j][1]) {
                    answer[j]++;
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
//        int[][] score = new int[][]{{80, 70}, {70, 80}, {30, 50}, {90, 100}, {100, 90}, {100, 100}, {10, 30}};
        int[][] score = new int[][]{{0, 20}, {80, 100}, {10, 10}, {90, 90}, {20, 0}};
        int[] result = solution(score);
        for (int r : result) {
            System.out.println(r);
        }
    }
}