https://school.programmers.co.kr/learn/courses/30/lessons/42840
class Solution {
public int[] solution(int[] answers) {
int[] result = new int[3];
int[] pattern1 = {1,2,3,4,5}; // 5
int[] pattern2 = {2,1,2,3,2,4,2,5}; // 8
int[] pattern3 = {3,3,1,1,2,2,4,4,5,5}; // 10
int[] one = new int[answers.length];
int[] two = new int[answers.length];
int[] three = new int[answers.length];
for (int i=0; i<answers.length; i++) {
one[i] = pattern1[i % pattern1.length];
two[i] = pattern2[i % pattern2.length];
three[i] = pattern3[i % pattern3.length];
}
int count1 = 0;
int count2 = 0;
int count3 = 0;
// 정답 개수 세기
for (int i=0; i<answers.length; i++) {
if (one[i] == answers[i]) {
count1++;
}
if (two[i] == answers[i]) {
count2++;
}
if (three[i] == answers[i]) {
count3++;
}
}
// 가장 많이 맞힌 사람 구하기
int max = Math.max(count1, Math.max(count2, count3));
int index = 0;
// result 배열에 넣어주기
if (count1 == max) {
result[index++] = 1;
}
if (count2 == max) {
result[index++] = 2;
}
if (count3 == max) {
result[index++] = 3;
}
// 최종 결과 새로운 배열로 복사하기
int[] finalResult = new int[index];
for (int i=0; i<index; i++) {
finalResult[i] = result[i];
}
return finalResult;
}
}
어렵다,,,,,,,,,, 이번에도 chatgpt 도움 족음^^ 받아서 해결...
답변 패턴을 반복해서 넣어주는 부분을 모르겠어서 물어봤다.
이렇게 나머지를 이용해서 넣어주면 되는 거였다 !! 호오
for (int i=0; i<answers.length; i++) {
one[i] = pattern1[i % pattern1.length];
two[i] = pattern2[i % pattern2.length];
three[i] = pattern3[i % pattern3.length];
}
근데 다른 사람 풀이 보니까 굳이 수포자 1,2,3 답변 배열 안 만들어도 풀 수 있는 거였다!!! 뭔가 나는 "수포자들 답변 배열을 만들어서 정답이랑 하나씩 비교해야겠다" 생각해서 만들었던 것 같다.
수포자들 배열 안 만든 버전 ⬇️
그나마 좀 간결해졌땅.
class Solution {
public int[] solution(int[] answers) {
int[] result = new int[3];
int[] one = {1,2,3,4,5}; // 5
int[] two = {2,1,2,3,2,4,2,5}; // 8
int[] three = {3,3,1,1,2,2,4,4,5,5}; // 10
int count1 = 0, count2 = 0, count3 =0;
// 정답 개수 세기
for (int i=0; i<answers.length; i++) {
if (answers[i] == one[i % one.length]) {
count1++;
}
if (answers[i] == two[i % two.length]) {
count2++;
}
if (answers[i] == three[i % three.length]) {
count3++;
}
}
// 가장 많이 맞힌 사람 구하기
int max = Math.max(count1, Math.max(count2, count3));
int index = 0;
// result 배열에 넣어주기
if (count1 == max) {
result[index++] = 1;
}
if (count2 == max) {
result[index++] = 2;
}
if (count3 == max) {
result[index++] = 3;
}
// 최종 결과 새로운 배열로 복사하기
int[] finalResult = new int[index];
for (int i=0; i<index; i++) {
finalResult[i] = result[i];
}
return finalResult;
}
}