https://school.programmers.co.kr/learn/courses/30/lessons/42840
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
[풀이과정]
1번 수포자 -> 1, 2, 3, 4, 5 반복 (5개)
2번 수포자 -> 2, 1, 2, 3, 2, 4, 2, 5 반복 (8개)
3번 수포자 -> 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 반복 (10개)
-> 배열로 만들어 사용
입력된 배열을 전부 탐색하며 1, 2, 3번 수포자의 답과 비교해준다.
시간복잡도 O(3N)
1. 입력된 배열의 인덱스를 (index % 수포자 당 반복되는 답의 개수) 해준 후
2. 만든 배열의 해당 값과 같은 인덱스에 있는 값과 비교해준다.
[코드]
<수포자번호, 맞은 개수>를 오름차순으로 정렬해서 풀까 했는데
중간에 요소를 추가할 수 있는 리스트가 아닌 배열로 반환해야 하기 때문에 크기를 미리 정해서 초기화 해줘야 해서 고민하다가 어차피 수포자 3명이라 반환형식이 7가지여서 if-else 문으로 풀었다. 코드가 마음에 들지 않아서 좀 찾아보니 깔끔한 코드들은 모두 stream을 사용했다.
import java.util.Arrays;
class Solution {
//수포자 당 반복되는 답 양상
static int[] number_1 = {1, 2, 3, 4, 5 };
static int[] number_2 = {2, 1, 2, 3, 2, 4, 2, 5};
static int[] number_3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
static int answer_1 = 0;
static int answer_2 = 0;
static int answer_3 = 0;
public int[] solution(int[] answers) {
for(int i = 0; i < answers.length; i++){
//현재 답과 비교할 반복되는 답의 index 구하기
int first = i % number_1.length;
int second = i % number_2.length;
int third = i % number_3.length;
if(answers[i] == number_1[first]){
answer_1++;
}
if(answers[i] == number_2[second]){
answer_2++;
}
if(answers[i] == number_3[third]){
answer_3++;
}
}
int max = Math.max(answer_1, Math.max(answer_2, answer_3));
if(max == answer_1 && max == answer_2 && max == answer_3){
return new int[]{1, 2, 3};
}
else if(max == answer_1 && max == answer_2){
return new int[]{1, 2};
}
else if(max == answer_1 && max == answer_3){
return new int[]{1, 3};
}
else if(max == answer_2 && max == answer_3){
return new int[]{2, 3};
}
else if(max == answer_1){
return new int[]{1};
}
else if(max == answer_2){
return new int[]{2};
}
else {
return new int[]{3};
}
}
}
[다른 사람 풀이 1]
list를 배열로 바꿔 반환
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();
}
}
[chatGPT 풀이]
intStream 사용.
: IntStream.range(0, 3).filter(i -> scores[i] == maxScore).map(i -> i+1).toArray();
0,1, 2의 숫자(i) 중
scores[i] == maxScore인 숫자를 뽑아서
+1한 값을
배열로 반환한다.
import java.util.*;
import java.util.stream.IntStream;
class Solution {
int[][] patterns = {
{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) {
int[] scores = new int[3];
for(int i = 0; i < answers.length; i++){
for(int j = 0; j < 3; j++){
if(answers[i] == patterns[j][i % patterns[j].length]){
scores[j]++;
}
}
}
int maxScore = Arrays.stream(scores).max().getAsInt();
return IntStream.range(0, 3).filter(i -> scores[i] == maxScore).map(i -> i+1).toArray();
}
}
[느낀점&배운점]
알고리즘에도 stream을 사용해서 람다식으로 표현하면 코드가 훨씬 간결해져서 좋은 것 같다.
하지만 stream을 사용하면 실행속도에 영향을 끼치기 때문에 간결성과 실행속도의 트레이드오프를 고려하여 사용하는 것이 좋다.
'프로그래머스 > 완전탐색' 카테고리의 다른 글
[프로그래머스] Level 2. 카펫 (0) | 2024.04.11 |
---|---|
[프로그래머스] Level 2. 소수 찾기 (0) | 2024.04.08 |
[프로그래머스] Level 1. 최소직사각형 (0) | 2024.03.30 |