풀이 |
1. 학생 세 명의 정답 패턴을 각각 배열에 저장한다 2. list를 두 개 선언한다 - list는 모든 값을 저장하는 용도 - list2는 가장 큰 값을 가지는 사람의 번호를 저장하는 용도로 쓰인다 3. 함수 check에서 no[i%no.length]의 값과 정답의 값이 같다면 score를 증가시킨다 4. 학생 순서대로 check를 호출해서 list에 저장한다 5. list의 가장 큰 값을 max에 저장한 후, 반복문으로 정답의 수와 max값이 같다면 list2에 i+1을 추가한다 6. list2를 배열로 바꿔 반환한다 |
회고 |
- list의 가장 큰 값 = Collections.max - list를 배열로 : int[] array = list.stream().mapToInt(Integer::intValue).toArray(); |
코드 (접은 글) |
import java.util.*;
class Solution {
static int[] no1 = {1,2,3,4,5};
static int[] no2 = {2,1,2,3,2,4,2,5};
static int[] no3 = {3,3,1,1,2,2,4,4,5,5};
public int[] solution(int[] answers) {
List<Integer> list = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list.add(check(answers, no1));
list.add(check(answers, no2));
list.add(check(answers, no3));
int max = Collections.max(list);
for(int i=0;i<3;i++){
if (list.get(i)==max)
list2.add(i+1);
}
int[] answer = list2.stream().mapToInt(Integer::intValue).toArray();
return answer;
}
public static int check(int[] answers, int[] no){
int score=0;
for(int i=0;i<answers.length;i++){
int a = answers[i];
if(a==no[i%no.length])
score++;
}
return score;
}
}
'코테 > 구현' 카테고리의 다른 글
[C/C++] 프로그래머스스쿨 외벽 점검, 구현 (0) | 2023.02.26 |
---|---|
[C/C++] 백준 치킨 배달, 구현 (0) | 2023.02.26 |
[C/C++] 프로그래머스 스쿨 기둥과 보, 구현 (0) | 2023.02.26 |
[C/C++] 프로그래머스스쿨 자물쇠와 열쇠, 구현 (0) | 2023.02.26 |
[C/C++] 백준 3190, 구현 (0) | 2023.02.18 |