본문 바로가기
개발로그/알고리즘

프로그래머스 Lv.0 등수매기기

by 쩜징 2023. 7. 23.

프로그래머스 Lv.0 등수매기기


영어 점수와 수학 점수의 평균 점수를 기준으로 등수를 매긴다.

영어 점수와 수학 점수를 담은 2차원 배열 score가 주어지고,

평균을 기준으로 등수를 담은 배열을 return하라.

 

score는 중복된 원소를 갖지 않는다.

 

** 풀이 방법

 

Double형 ArrayList를 생성한다.

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

 

등수를 리턴할 answer배열을 모두 score.length로 채운다.

Arrays.fill(answer, score.length);

 

score를 조회해서 두 점수의 평균을 ArrayList에 저장한다.

for (int i=0; i<score.length; i++) {
	list.add((score[i][0]+score[i][1])/2.0);
    //score[i][0] = 영어 점수, score[i][1] = 수학 점수
}

 

이중반복문을 통해 list의 값들을 비교한다.

인덱스가 같지 않으면서 i인덱스의 값이 j인덱스의 값보다 같거나 작으면등수를 -1씩 감소시킨다.

for (int i=0; i<list.size(); i++) {
	for (int j=0; j<list.size(); j++) {
    	   if (i!=j && list.get(i)<=list.get(j))
        	answer[j] -= 1;
    }
}

 

풀이 과정 예시

score result
[[80, 70], [90, 50], [40,70], [50, 80]] [1, 2, 4, 3]

 

<> 전체 코드 </>

import java.util.*;
class Solution {
    public int[] solution(int[][] score) {
        int[] answer = new int [score.length];
        List<Double> list = new ArrayList<>();
    
        Arrays.fill(answer, score.length);
        
        for (int i=0; i<score.length; i++) {
            list.add((score[i][0]+score[i][1])/2.0);
        }

        for(int i=0;i<list.size();i++){
            for(int j=0;j<list.size();j++){
                if(i!=j&&list.get(i)<=list.get(j)) answer[j]-=1;
            }
        }
        return answer;
    }
}

반응형

댓글