본문 바로가기
JAVA 코딩 알고리즘 연습/프로그래머스

프로그래머스 - 모의고사 / JAVA (자바) 코딩 알고리즘 연습

by easpop 2022. 12. 15.
728x90
반응형
반응형

로직을 짤 때, 찍는 방식 반복하는걸 어떻게 처리해야 될지 몰라서 구글링 해봤던 문제ㅜㅜ

학생별로 배열을 만들어서 각 배열의 길이로 나눈 나머지 값으로 처리하면 됨!

score 배열을 만들어서 각 문제별로 맞췄을 경우, score 배열에 추가

최대값 중복되는거 있으면 전부 리턴~~ 코드 자체는 그렇게 어렵지 않았다

import java.util.ArrayList;

class Solution {
    public int[] solution(int[] answers) {
        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 < answers.length; i++) {
			if(answers[i] == a[i%5]) score[0]++;
			if(answers[i] == b[i%8]) score[1]++;
			if(answers[i] == c[i%10]) score[2]++;
		}
		
		int max = Math.max(score[0], Math.max(score[1], score[2]));
		
		ArrayList<Integer> arr = new ArrayList<>();
		for (int i = 0; i < score.length; i++) {
			if(max == score[i]) {
				arr.add(i+1);
			}
		}
		
		int[] answer = new int[arr.size()];
		for (int i = 0; i < arr.size(); i++) {
			answer[i] = arr.get(i);
		}
        
        return answer;
    }
}
728x90
반응형

댓글