수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.
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, ...
1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요.
제한 조건
- 시험은 최대 10,000 문제로 구성되어있습니다.
- 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
- 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.
answers return
[1,2,3,4,5] | [1] |
[1,3,2,4,2] | [1,2,3] |
입출력 예 #1
- 수포자 1은 모든 문제를 맞혔습니다.
- 수포자 2는 모든 문제를 틀렸습니다.
- 수포자 3은 모든 문제를 틀렸습니다.
따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.
입출력 예 #2
- 모든 사람이 2문제씩을 맞췄습니다.
<내풀이>
수포자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}
문제의 수많은 for문을 돌립니다.
각각의 수포자의 문제 맞춘 갯수를 담을 변수를 각각 선언해 줍니다.
for문 안에서 수포자들의 답을 적는 패턴이 있는데, 그 수만큼 다 돌면 다시 초기화시켜줍니다.
- 가장 많이 맞춘 사람을 리턴해줍니다.
- 맞춘 갯수가 같을 경우에는 다 리턴해줍니다.
public static int[] solution(int[] answers) {
int[] answer = null;
int[] math1 = { 1, 2, 3, 4, 5 };
int[] math2 = { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] math3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int math1ResultNumber = 0;
int math2ResultNumber = 0;
int math3ResultNumber = 0;
int math1Count = 0;
int math2Count = 0;
int math3Count = 0;
for (int i = 0; i < answers.length; i++) {
if (answers[i] == math1[math1Count]) {
math1ResultNumber++;
}
;
if (math1Count < math1.length - 1) {
math1Count++;
} else {
math1Count = 0;
}
if (answers[i] == math2[math2Count]) {
math2ResultNumber++;
}
;
if (math2Count < math2.length - 1) {
math2Count++;
} else {
math2Count = 0;
}
if (answers[i] == math3[math3Count]) {
math3ResultNumber++;
}
;
if (math3Count < math3.length - 1) {
math3Count++;
} else {
math3Count = 0;
}
}
if (math1ResultNumber == math2ResultNumber && math1ResultNumber == math3ResultNumber) {
answer = new int[3];
answer[0] = 1;
answer[1] = 2;
answer[2] = 3;
} else if (math1ResultNumber == math2ResultNumber && math2ResultNumber > math3ResultNumber) {
answer = new int[2];
answer[0] = 1;
answer[1] = 2;
} else if (math2ResultNumber == math3ResultNumber && math3ResultNumber > math1ResultNumber) {
answer = new int[2];
answer[0] = 2;
answer[1] = 3;
} else if (math3ResultNumber == math1ResultNumber && math1ResultNumber > math2ResultNumber) {
answer = new int[2];
answer[0] = 1;
answer[1] = 3;
} else if (math1ResultNumber > math2ResultNumber && math1ResultNumber > math3ResultNumber) {
answer = new int[1];
answer[0] = 1;
} else if (math2ResultNumber > math3ResultNumber && math2ResultNumber > math1ResultNumber) {
answer = new int[1];
answer[0] = 2;
} else if (math3ResultNumber > math2ResultNumber && math3ResultNumber > math1ResultNumber) {
answer = new int[1];
answer[0] = 3;
}
return answer;
}
(그냥 희식의 흐름대로 다했다........ 노가다였다. 머리를 써야한다.)
<남풀이_1>
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();
}
}
(코드는 간결하나, 시간이 많이 걸린다고합니다. 아래 max 넘버를 찾는 부분을 참고할만 할것 같다.)
<남풀이_2>
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] arr = new int[3];
int[] a = {5,1,2,3,4};
int[] b = {5,2,1,2,3,2,4,2};
int[] c = {5,3,3,1,1,2,2,4,4,5};
for(int i = 0; i < answers.length; i++){
if(answers[i] == a[(i+1)%5]) arr[0]++;
if(answers[i] == b[(i+1)%8]) arr[1]++;
if(answers[i] == c[(i+1)%10]) arr[2]++;
}
int max = arr[0];
if(max < arr[1]) max = arr[1];
if(max < arr[2]) max = arr[2];
if(max == arr[0] && max == arr[1] && max == arr[2]) return new int[]{1,2,3};
else if(max == arr[0] && max == arr[1]) return new int[]{1,2};
else if(max == arr[0] && max == arr[2]) return new int[]{1,3};
else if(max == arr[1] && max == arr[2]) return new int[]{2,3};
else if(max == arr[0]) return new int[]{1};
else if(max == arr[1]) return new int[]{2};
return new int[]{3};
}
}
(나는 수포자의 찍는 정답의 갯수가 되면 다시 0으로 만들어줬는데, 그럴 필요없이 그냥 찍는 정답의 갯수로 나눴을때 나머지를 인덱스로 설정하면 참 편하게 코드를 짤 수 있습니다.)
'컴퓨터 프로그래밍 > 알고리즘' 카테고리의 다른 글
프로그래머스_소수찾기 (0) | 2020.05.23 |
---|---|
프로그래머스_완주하지 못한 선수 (0) | 2020.05.22 |
프로그래머스_나누어 떨어지는 숫자 배열 (0) | 2020.05.21 |
프로그래머스_하샤드수 (0) | 2020.05.20 |
프로그래머스_K번째수 (0) | 2020.05.20 |