🚩 코딩테스트/알고리즘

[프로그래머스] 모의고사

딩딩크롱 2022. 9. 5. 22:45
728x90

문제

https://school.programmers.co.kr/learn/courses/30/lessons/86491

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

코드

자바
import java.util.*;

class Solution {
    public int[] solution(int[] answers) {
        int[][] nums = { { 1, 2, 3, 4, 5 }, { 2, 1, 2, 3, 2, 4, 2, 5 }, { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 } };
        int[] n = { 5, 8, 10 };
        int[] count = new int[3];
        
        for (int i = 0; i < answers.length; i++) {
            for (int j = 0; j < 3; j++) {
                if (answers[i] == nums[j][i % n[j]])
                    count[j]++;
            }
        }
        
        int max = 0;
        for (int i = 0; i < count.length; i++) {
            max = Math.max(max, count[i]);
        }
        
        ArrayList<Integer> tmp = new ArrayList<>();
        for (int i = 0; i < count.length; i++) {
            if (max == count[i])
                tmp.add(i + 1);
        }
        
        int[] result = new int[tmp.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = tmp.get(i);
        }
        
        return result;
    }
}
파이썬
def solution(answers):
    nums = [[1, 2, 3, 4, 5], [2, 1, 2, 3, 2, 4, 2, 5], [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]]
    n = [5, 8, 10]
    result = [0] * 3
    
    for i, answer in enumerate(answers):
        for j in range(3):
            if nums[j][i % n[j]] == answer:
                result[j] += 1
                
    top = max(result)
    return [i + 1 for i in range(3) if result[i] == top]
728x90