https://programmers.co.kr/learn/courses/30/lessons/42840?language=java
코딩테스트 연습 - 모의고사
수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는
programmers.co.kr
사람 수가 3명이라고 제한되어서 쉽게 해결이 가능했다.
(ArrayList, Modular연산자 사용)
import java.util.ArrayList;
class Solution
{
public static int[] solution(int[] answers)
{
int[] one = new int[]{1,2,3,4,5};
int[] two = new int[]{2,1,2,3,2,4,2,5};
int[] three = new int[]{3,3,1,1,2,2,4,4,5,5};
int one_cnt = 0;
int two_cnt = 0;
int three_cnt = 0;
List<Integer> list = new ArrayList<Integer>();
int max = Integer.MIN_VALUE;
for(int i=0; i<answers.length; i++)
{
if(answers[i]==one[i%5])
{
one_cnt++;
}
if(answers[i]==two[i%8])
{
two_cnt++;
}
if(answers[i]==three[i%10])
{
three_cnt++;
}
}
max = Math.max(max, one_cnt);
max = Math.max(max, two_cnt);
max = Math.max(max, three_cnt);
if(max==one_cnt)
{
list.add(1);
}
if(max==two_cnt)
{
list.add(2);
}
if(max==three_cnt)
{
list.add(3);
}
int[] result = new int[list.size()];
for(int i=0; i<list.size(); i++)
{
result[i] = list.get(i);
}
return result;
}
}
'[ 다먹살 ] > - Coding' 카테고리의 다른 글
[프로그래머스] 레벨1 소수만들기 (0) | 2021.07.22 |
---|---|
[구름] 레벨3 계단오르기 (0) | 2021.07.22 |
[프로그래머스] 레벨2 올바른 괄호 (0) | 2021.07.22 |
[프로그래머스] 레벨2 카펫 (0) | 2021.07.22 |
[구름] 레벨1 피라미드 (0) | 2021.07.21 |
댓글