yesolje
프로그래머스_고득점kit_완전탐색_모의고사 본문
풀이
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] supo1 = {1,2,3,4,5};
int[] supo2 = {2,1,2,3,2,4,2,5};
int[] supo3 = {3,3,1,1,2,2,4,4,5,5};
HashMap<Integer,Integer> score = new HashMap<>();
score.put(1,0);
score.put(2,0);
score.put(3,0);
for (int i = 0; i < answers.length; i++) {
if (answers[i] == supo1[i % supo1.length]) {
score.put(1, score.get(1) + 1);
}
if (answers[i] == supo2[i % supo2.length]) {
score.put(2, score.get(2) + 1);
}
if (answers[i] == supo3[i % supo3.length]) {
score.put(3, score.get(3) + 1);
}
}
int highestScore = Collections.max(score.values());
// 최고 점수를 받은 사람 찾기
List<Integer> topScorers = new ArrayList<>();
for (int key : score.keySet()) {
if (score.get(key) == highestScore) {
topScorers.add(key);
}
}
return topScorers.stream().mapToInt(i -> i).toArray();
}
}
✔️ 포인트 1 : 끝에 도달하면 처음으로 돌아가는 노드
피험자는 각각 다른 크기의 반복 패턴을 가진다. 문항 체크가 피험자마다 다르게 반복될 수 있도록 나머지를 사용하여 노드의 반복을 구현한다.
✔️ 포인트 2: 가장 높은 점수를 찾고, 그 점수를 가진 사람을 찾기
최고득점자 기준, 동점자가 있을 시 오름차순 정렬을 하기 위해 Collection.max(score.values()); 를 사용하였다.
'코딩테스트' 카테고리의 다른 글
| 프로그래머스_고득점kit_우선탐색_타겟넘버 (0) | 2025.03.11 |
|---|---|
| 프로그래머스_고득점kit_그리디_체육복 (1) | 2025.03.11 |
| 프로그래머스_고득점kit_완전탐색_최소직사각형 (0) | 2025.03.07 |
| 프로그래머스_고득점kit_정렬_K번째 수 (0) | 2025.03.06 |
| 프로그래머스_고득점kit_스택큐_같은 숫자는 싫어 (0) | 2025.03.06 |