사용 알고리즘: 완탐..이라고 하기도 참 그래
사용 언어: java
import java.util.*;
//1번: 1 2 3 4 5 반복
//2번: 2 1 2 3 2 4 2 5 반복
//3번: 3 3 1 1 2 2 4 4 5 5 반복
class Solution {
static int[][] a = new int[4][];
// static int[] a1 = new int {1,2,3,4,5};
// static int[] a2 = new int {2,1,2,3,2,4,2,5};
// static int[] a3 = new int {3,3,1,1,2,2,4,4,5,5};
public int[] solution(int[] answers) {
a[1] = new int[] {1,2,3,4,5};
a[2] = new int[] {2,1,2,3,2,4,2,5};
a[3] = new int[] {3,3,1,1,2,2,4,4,5,5};
int[] answerCnt = new int[4];
for(int i=1; i<4; i++) {
answerCnt[i] = cntCorrect(answers, a[i]);
}
// System.out.println(Arrays.toString(answerCnt));
int max = answerCnt[1];
ArrayList<Integer> answer = new ArrayList<>();
answer.add(1);
for(int i=2; i<4; i++) {
if(max == answerCnt[i])
answer.add(i);
if(max < answerCnt[i]) {
max = answerCnt[i];
answer.clear();
answer.add(i);
}
}
return answer.stream().mapToInt(Integer::intValue).toArray();
}
static int cntCorrect(int[] answers, int[] jjik) {
int correct = 0;
int size = jjik.length;
int idx = 0;
for(int i=0; i<answers.length; i++) {
if(answers[i] == jjik[idx]) correct++;
idx = (idx+1) % size;
}
return correct;
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
미로 탈출 명령어 *** (0) | 2024.09.11 |
---|---|
부모의 형질을 모두 가지는 대장균 찾기 - (0) | 2024.09.02 |
기능개발 (0) | 2024.08.30 |
상담원 인원 **** (0) | 2024.08.27 |
n + 1 카드게임 *(후우..) (0) | 2024.08.23 |