문제 출처: https://programmers.co.kr/learn/courses/30/lessons/42840
1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
위와 같은 방식으로 찍는 세명의 케이스가 있다.
문제의 해답이 주어졌을때 가장 많이 맞춘사람을 return 하면 된다.
동일할 경우 동점자 포함 모두 return 하면됨. 상세 내용은 출처 참조.
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] answerss = {1,2,3};
int[] numOfCor = {0,0,0};
int i = 0;
int num1Index = 0;
int num2Index = 0;
int num3Index = 0;
int [] num1 = {1,2,3,4,5};
int [] num2 = {2,1,2,3,2,4,2,5};
int [] num3 = {3,3,1,1,2,2,4,4,5,5};
while(i < answers.length) {
int temp1 = i % num1.length;
int temp2 = i % num2.length;
int temp3 = i % num3.length;
if(temp1 != 0) {
if(answers[i] == num1[num1Index])
numOfCor[0]++;
} else {
num1Index = 0;
if(answers[i] == num1[num1Index])
numOfCor[0]++;
}
if(temp2 != 0) {
if(answers[i] == num2[num2Index])
numOfCor[1]++;
} else {
num2Index = 0;
if(answers[i] == num2[num2Index])
numOfCor[1]++;
}
if(temp3 != 0) {
if(answers[i] == num3[num3Index])
numOfCor[2]++;
} else {
num3Index = 0;
if(answers[i] == num3[num3Index])
numOfCor[2]++;
}
i++;
num1Index++;
num2Index++;
num3Index++;
}
int max = numOfCor[0];
if (max > numOfCor[1] && max > numOfCor[2]) {int[] answer = {1};return answer;}
else if (max > numOfCor[1] && max == numOfCor[2]){int[] answer = {1, 3};return answer;}
else if (max == numOfCor[1] && max > numOfCor[2]){int[] answer = {1, 2};return answer;}
else if (max < numOfCor[1] && numOfCor[1] > numOfCor[2]){int[] answer = {2};return answer;}
else if (max < numOfCor[1] && numOfCor[1] == numOfCor[2]){int[] answer = {2, 3};return answer;}
else if (numOfCor[2] > numOfCor[1] && max < numOfCor[2]){int[] answer = {3};return answer;}
else if (max == numOfCor[1] && max == numOfCor[2]){int[] answer = {1,2,3};return answer;}
return answerss;
}
}
패턴을 전부 찾아서 세가지 케이스의 정답 갯수를 각각 구하고, 나올 수 있는 답의 가지 수를 전부 if문으로 만들어서 무식하게 풀었습니다.
import java.util.ArrayList;
class Solution {
public int[] solution(int[] answer) {
int[] a = {1, 2, 3, 4, 5};
int[] b = {2, 1, 2, 3, 2, 4, 2, 5};
int[] c = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int[] score = new int[3];
for(int i=0; i<answer.length; i++) {
if(answer[i] == a[i%a.length]) {score[0]++;}
if(answer[i] == b[i%b.length]) {score[1]++;}
if(answer[i] == c[i%c.length]) {score[2]++;}
}
int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
ArrayList<Integer> list = new ArrayList<>();
if(maxScore == score[0]) {list.add(1);}
if(maxScore == score[1]) {list.add(2);}
if(maxScore == score[2]) {list.add(3);}
return list.stream().mapToInt(i->i.intValue()).toArray();
}
}
비슷한 방식인데 스트림을 사용하였고 코드가 간결하군요.
import java.util.*;
class Solution {
public static int[] solution(int[] answers) {
int[][] patterns = {
{1, 2, 3, 4, 5},
{2, 1, 2, 3, 2, 4, 2, 5},
{3, 3, 1, 1, 2, 2, 4, 4, 5, 5}
};
int[] hit = new int[3];
for(int i = 0; i < hit.length; i++) {
for(int j = 0; j < answers.length; j++) {
if(patterns[i][j % patterns[i].length] == answers[j]) hit[i]++;
}
}
int max = Math.max(hit[0], Math.max(hit[1], hit[2]));
List<Integer> list = new ArrayList<>();
for(int i = 0; i < hit.length; i++)
if(max == hit[i]) list.add(i + 1);
int[] answer = new int[list.size()];
int cnt = 0;
for(int num : list)
answer[cnt++] = num;
return answer;
}
}
진짜 비슷한 방식인데 코드가 엄청 간결합니다.....이건 최고네요.