프로그래머스 코딩테스트 입문 등수 매기기 [JAVA] - 22년 10월 15일

Denia·2022년 10월 14일
0

코딩테스트 준비

목록 보기
98/201
import java.util.ArrayList;
import java.util.List;

class Solution {
    public int[] solution(int[][] score) {
        int[] answer = new int[score.length];

        List<StudentScore> list = new ArrayList<StudentScore>();

        for (int i = 0; i < score.length; i++) {
            int[] ints = score[i];
            list.add(new StudentScore(ints[0], ints[1], i));
        }

        list.sort(null);

        double tempScore = list.get(0).getAvg();
        int tempRank = 1;
        int tempCount = 1;

        for (StudentScore studentScore : list) {
            if (tempScore > studentScore.getAvg()) {
                tempScore = studentScore.getAvg();
                tempRank = tempCount;
            }

            int tempIndex = studentScore.getIndex();
            answer[tempIndex] = tempRank;

            tempCount++;
        }

        return answer;
    }
}

class StudentScore implements Comparable<StudentScore> {
    private final int engScore, mathScore, index;
    private final double avg;

    public StudentScore(int engScore, int mathScore, int index) {
        this.engScore = engScore;
        this.mathScore = mathScore;
        this.index = index;

        this.avg = (engScore + mathScore) / 2.0;
    }

    public int getIndex() {
        return index;
    }

    public double getAvg() {
        return avg;
    }

    @Override
    public int compareTo(StudentScore o1) {
        double avg1 = this.getAvg();
        double avg2 = o1.getAvg();

        if (avg1 > avg2) return -1;
        else if (avg1 < avg2) return 1;

        return 0;
    }
}

profile
HW -> FW -> Web

0개의 댓글