문제
제한 조건
- 시험은 최대 10,000 문제로 구성되어있습니다.
- 문제의 정답은 1, 2, 3, 4, 5중 하나입니다.
- 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해주세요.
입출력 예
answersreturn
[1,2,3,4,5] | [1] |
[1,3,2,4,2] | [1,2,3] |
입출력 예 설명
입출력 예 #1
- 수포자 1은 모든 문제를 맞혔습니다.
- 수포자 2는 모든 문제를 틀렸습니다.
- 수포자 3은 모든 문제를 틀렸습니다.
따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.
입출력 예 #2
- 모든 사람이 2문제씩을 맞췄습니다.
import java.util.*;
class Solution {
public ArrayList<Integer> solution(int[] answers) {
int[] answer = {};
int[] person1 = {1,2,3,4,5};
int[] person2 = {2,1,2,3,2,4,2,5};
int[] person3 = {3,3,1,1,2,2,4,4,5,5};
ArrayList<Integer> problems = new ArrayList<>();
for(int a : answers) problems.add(a);
int p1 = 0;
int p2 = 0;
int p3 = 0;
for(int i =0; i<problems.size();i++){
if(problems.get(i)==person1[i%5]) p1++;
if(problems.get(i)==person2[i%8]) p2++;
if(problems.get(i)==person3[i%10]) p3++;
};
int[] aw = {p1,p2,p3};
int big = Math.max(p1 , Math.max(p2,p3));
ArrayList<Integer> ans = new ArrayList<>();
for(int i =0; i<3; i++){
if(aw[i]==big){
ans.add(i+1);
}
}
return ans;
}
}
- Math.max(a,b) : a,b 중 최댓값
- ArrayList to intArray
int[] arr = list.stream().mapToInt(i -> i).toArray();
'알고리즘' 카테고리의 다른 글
알고리즘 - 프로그래머스(숫자 문자열과 영단어) (0) | 2022.08.11 |
---|---|
알고리즘 - 프로그래머스(신규아이디 추천) (0) | 2022.08.09 |
알고리즘 - 프로그래머스(더 맵게) (0) | 2022.07.15 |
알고리즘 - 프로그래머스(폰켓몬) (0) | 2022.07.12 |
알고리즘 - 프로그래머스 K번째수 (0) | 2022.07.08 |