본문 바로가기
공부/알고리즘

[프로그래머스]42840_모의고사_완전탐색 Java 풀이

by happyeuni 2023. 2. 15.

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

 

프로그래머스

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

programmers.co.kr

 

정답은 길이를 알 수 없는 타입인데 반환 타입이 int[] 여서 곤란했다.

그래서 두번째 코드에는 좀 안예쁘게 되있는데 프로그래머스에서 solution 옆에 반환형을 내맘대로 바꿔도 정답으로 인정해주어 더 깔끔한 코드(첫번째에 있는 코드)로 바꿨다. 히히

 

풀이 코드

import java.util.*;

class Solution {
    public ArrayList<Integer> solution(int[] answers) {
        int[] answerNum = new int[4]; // 1번수포자,2번수포자,3번수포자는 각각의 번호가 인덱스로 - 맞은 개수 저장.
        
        int[] one = {1, 2, 3, 4, 5};
        int[] two = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] three = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    
        for(int i = 0;i<answers.length; i++){
            if(one[i%one.length] == answers[i]){
                answerNum[1]++;
            }
            if(two[i%two.length] == answers[i]){
                answerNum[2]++;
            }
            if(three[i%three.length] == answers[i]){
                answerNum[3]++;
            }
        }
        
        int max = Math.max(Math.max(answerNum[1],answerNum[2]),answerNum[3]);
        
        ArrayList<Integer> answer = new ArrayList<>();
        for(int i = 1; i <= 3; i++){
            if(answerNum[i] == max) answer.add(i);
        }
        
        return answer;
    }
}
import java.util.*;
import java.lang.*;

class Solution {
    public int[] solution(int[] answers) {
        int[] answerNum = new int[4]; // 1번수포자,2번수포자,3번수포자는 각각의 번호가 인덱스로 - 맞은 개수 저장.
        
        int[] one = {1, 2, 3, 4, 5};
        int[] two = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] three = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    
        for(int i = 0;i<answers.length; i++){
            if(one[i%one.length] == answers[i]){
                answerNum[1]++;
            }
            if(two[i%two.length] == answers[i]){
                answerNum[2]++;
            }
            if(three[i%three.length] == answers[i]){
                answerNum[3]++;
            }
        }
        int max = Math.max(Math.max(answerNum[1],answerNum[2]),answerNum[3]);
        int len = 0;
        for(int i = 1; i <= 3; i++){
            if(answerNum[i] == max) len++;
        }
        
        int [] answer = new int[len];
        int index=0;
        for(int i = 1; i <= 3; i++){
            if(index == len) break;
            if(answerNum[i] == max){
                answer[index] = i;
                index++;
            } 
        }
        return answer;
    }
}

 

 

다른 사람 풀이

- 역시나 int[] 반환 형이어서 그걸 해결하기 위해

list.stream().mapToInt(i->i.intValue()).toArray(); 를 써준 것을 볼 수 있다.

import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answer) {
        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<answer.length; i++) {
            if(answer[i] == a[i%a.length]) {score[0]++;}
            if(answer[i] == b[i%b.length]) {score[1]++;}
            if(answer[i] == c[i%c.length]) {score[2]++;}
        }
        int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
        ArrayList<Integer> list = new ArrayList<>();
        if(maxScore == score[0]) {list.add(1);}
        if(maxScore == score[1]) {list.add(2);}
        if(maxScore == score[2]) {list.add(3);}
        return list.stream().mapToInt(i->i.intValue()).toArray();
    }
}

댓글