https://school.programmers.co.kr/learn/courses/30/lessons/42840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 풀이
완전탐색 문제로 각 사람마다 정해진 정답 규칙을 통해 가장 많이 맞춘 사람의 번호를 찾는다.
이때 정답 규칙을 순회하기 위해 %를 사용해주면 된다.
코드
import java.util.*;
class Solution {
static int[][] peoples = new int[][]{
{},
{1,2,3,4,5},
{2,1,2,3,2,4,2,5},
{3,3,1,1,2,2,4,4,5,5}
};
public int[] solution(int[] answers) {
List<Integer> answer = new ArrayList<>();
int maxAnswer = 0;
List<Integer> answerCnt = new ArrayList<>();
for (int people = 1; people < peoples.length; people++) {
int cnt = 0;
int orderLen = peoples[people].length;
for (int i = 0; i < answers.length; i++) {
if (answers[i] == peoples[people][i % orderLen])
cnt++;
}
answerCnt.add(cnt);
maxAnswer = java.lang.Math.max(maxAnswer, cnt);
}
for (int i = 0; i < answerCnt.size(); i++) {
if (answerCnt.get(i) == maxAnswer)
answer.add(i+1);
}
return answer.stream().mapToInt(Integer::intValue).toArray();
}
}
'Coding Test > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 피로도 - 자바 (0) | 2023.08.10 |
---|---|
[프로그래머스] 소수 찾기 - 자바 (0) | 2023.08.10 |
[프로그래머스] 최소직사각형 - 자바 (0) | 2023.08.10 |
[프로그래머스] 더 맵게 - 자바 (0) | 2023.08.10 |
[프로그래머스] 주식 가격 - 자바 (0) | 2023.08.10 |