728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42840
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
728x90
메모리: 86.5 MB, 시간: 1.57 ms
사용 알고리즘: 완전탐색
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
// 1, 2, 3번 수포자가 찍는 방식을 담은 배열
int[][] pick = new int[][] {
{1, 2, 3, 4, 5},
{2, 1, 2, 3, 2, 4, 2, 5},
{3, 3, 1, 1, 2, 2, 4, 4, 5, 5}
};
// 점수
int[] scores = new int[3];
for(int i = 0; i < answers.length; i++) {
for(int j = 0; j < 3; j++) {
if(answers[i] == pick[j][i % pick[j].length]) scores[j]++;
}
}
// 가장 높은 점수를 받은 사람 구하기
int maxScore = scores[0];
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
for(int i = 1; i < 3; i++) {
if(maxScore < scores[i]) {
maxScore = scores[i];
list.clear();
list.add(i + 1);
}
else if(maxScore == scores[i])
list.add(i + 1);
}
// 가장 높은 점수를 받은 사람 리스트를 배열로 변경
int[] answer = new int[list.size()];
for(int i = 0; i < list.size(); i++) answer[i] = list.get(i);
return answer;
}
}
728x90
'Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스, 12921] 소수 찾기 (java) (0) | 2025.02.03 |
---|---|
[프로그래머스, 135808] 과일 장수 (java) (0) | 2025.02.01 |
[프로그래머스, 181893] 배열 조각하기 (java) (0) | 2025.02.01 |
[프로그래머스, 136798] 기사단원의 무기 (java) (0) | 2025.01.22 |
[프로그래머스, 340199] [PCCE 기출문제] 9번 / 지폐 접기 (java) (0) | 2025.01.22 |