데이터 엔지니어링 정복/Algorithm
[완전탐색] 프로그래머스 - 모의고사
noohhee
2021. 4. 11. 23:54
728x90
반응형
programmers.co.kr/learn/courses/30/lessons/42840
코딩테스트 연습 - 모의고사
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는
programmers.co.kr
내 풀이
더보기
import java.util.ArrayList;
class Solution {
public int[] solution(int[] answers) {
int[] answer = {};
int[] m1 = { 1, 2, 3, 4, 5 };
int[] m2 = { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] m3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int res1 = 0, res2 = 0, res3 = 0;
for( int i=0; i<answers.length; i++ ) {
if( m1[i % m1.length] == answers[i] ) res1++;
if( m2[i % m2.length] == answers[i] ) res2++;
if( m3[i % m3.length] == answers[i] ) res3++;
}
int max = Math.max( res1, Math.max( res2, res3) );
ArrayList<Integer> list = new ArrayList<Integer>();
if( max == res1 ) list.add(1);
if( max == res2 ) list.add(2);
if( max == res3 ) list.add(3);
answer = new int[ list.size() ];
for( int i=0; i<answer.length; i++ ) {
answer[i] = list.get(i);
}
return answer;
}
}
728x90
반응형