Java/프로그래머스
[JAVA] 프로그래머스 - 등수 매기기
쥬크버그
2024. 5. 11. 16:11
https://school.programmers.co.kr/learn/courses/30/lessons/120882
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr


ArrayList.indexOf()
Java - ArrayList.indexOf() 사용 방법 및 예제 (codechacha.com)
Java - ArrayList.indexOf() 사용 방법 및 예제
ArrayList의 indexOf()는 인자로 전달된 객체가 리스트에 존재한다면, 아이템의 인덱스를 리턴합니다. 앞쪽부터 인자와 동일한 객체가 있는지 찾으며, 존재한다면 그 인덱스를 리턴합니다. 없다면 `-1
codechacha.com
- indexOf(Object o)는 인자로 객체를 받는다. 리스트의 앞쪽부터 인자와 동일한 객체가 있는지 찾으며, 존재한다면 그 인덱스를 리턴한다. 존재하지 않는다면 -1을 리턴한다.
정답
import java.util.*;
class Solution {
public int[] solution(int[][] score) {
int[] answer = new int[score.length];
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<score.length; i++)
{
list.add(score[i][0]+score[i][1]);
}
//내림차순 정렬
list.sort(Comparator.reverseOrder());
for(int i=0; i<answer.length; i++)
{
answer[i] = list.indexOf(score[i][0]+score[i][1])+1;
}
return answer;
}
}
다른 사람의 풀이
class Solution {
public int[] solution(int[][] score) {
double[] map = new double[score.length];
int[] result= new int[score.length];
for (int i = 0; i <score.length ; i++) {
map[i] = (score[i][0]+ score[i][1])/2.0;
}
for (int i = 0; i <score.length ; i++) {
int count =0;
for (int j = 0; j <score.length ; j++) {
if(map[i] < map[j]){
count++;
}
}
result[i] = count+1;
}
return result;
}
}