프로그래머스 Lv.0 등수매기기
![](https://t1.daumcdn.net/keditor/emoticon/face/large/073.png)
영어 점수와 수학 점수의 평균 점수를 기준으로 등수를 매긴다.
영어 점수와 수학 점수를 담은 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;
}
}
반응형
'개발로그 > 알고리즘' 카테고리의 다른 글
프로그래머스 Lv.0 숨어있는 숫자의 덧셈 (2) (0) | 2023.07.24 |
---|---|
프로그래머스 Lv.0 연속된 수의 합 (0) | 2023.07.24 |
프로그래머스 Lv.0 안전지대 (0) | 2023.07.23 |
프로그래머스 Lv.0 진료순서 정하기 (0) | 2023.07.21 |
프로그래머스 Lv.0 가까운 수 (0) | 2023.07.20 |
댓글