티스토리 뷰
문제 링크
문제 조건
- 시험은 최대 10,000 문제로 구성되어 있습니다.
- 문제의 정답은 1, 2, 3, 4, 5 중 하나입니다.
- 가장 높은 점수를 받은 사람이 여럿일 경우, return하는 값을 오름차순 정렬해야 합니다.
변수 설명
- answers : 1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 (매개변수)
- one : 1번 수포자가 찍는 방식
- oneScore : 1번 수포자의 점수
- two : 2번 수포자가 찍는 방식
- twoScore : 2번 수포자의 점수
- three : 3번 수포자가 찍는 방식
- threeScore : 3번 수포자의 점수
- len : answer의 길이
코드 설명
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<len;i++){
if(answers[i] == one[i%5]) oneScore++;
if(answers[i] == two[i%8]) twoScore++;
if(answers[i] == three[i%10]) threeScore++;
}- [1]에서 구한 배열과 answer을 비교하여 수포자들의 점수를 구합니다.
int maxScore = Math.max(oneScore, Math.max(twoScore, threeScore));
ArrayList<Integer> list = new ArrayList<>();
if(maxScore == oneScore)list.add(1);
if(maxScore == twoScore)list.add(2);
if(maxScore == threeScore)list.add(3);
int lLen = list.size();
answer = new int[lLen];
for(int i=0;i< lLen;i++){
answer[i] = list.get(i);
}
}- maxScore를 구한 뒤, list에 넣어준 뒤 answer에 맞는 타입으로 변환하여 넣어줍니다.
문제 해답
import java.util.ArrayList;
class Solution {
public int[] solution(int[] answers) {
int[] answer;
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};
int oneScore = 0, twoScore = 0, threeScore = 0;
int len = answers.length;
for(int i=0;i<len;i++){
if(answers[i] == one[i%5]) oneScore++;
if(answers[i] == two[i%8]) twoScore++;
if(answers[i] == three[i%10]) threeScore++;
}
int maxScore = Math.max(oneScore, Math.max(twoScore, threeScore));
ArrayList<Integer> list = new ArrayList<>();
if(maxScore == oneScore)list.add(1);
if(maxScore == twoScore)list.add(2);
if(maxScore == threeScore)list.add(3);
int lLen = list.size();
answer = new int[lLen];
for(int i=0;i< lLen;i++){
answer[i] = list.get(i);
}
return answer;
}
}'알고리즘 > Programmers' 카테고리의 다른 글
| [Programmers] 고득점 Kit - Hash : 완주하지 못한 선수 #42576 (0) | 2020.01.28 |
|---|---|
| [Programmers] 고득점 Kit - Brute Force : 소수찾기 #42839 (0) | 2020.01.27 |
| [Programmers] 고득점 Kit - Stack/Queue : 주식가격 #42584 (0) | 2020.01.17 |
| [Programmers] 고득점 Kit - Stack/Queue : 쇠막대기 #42585 (0) | 2020.01.16 |
| [Programmers] 고득점 Kit - Stack/Queue : 프린터 #42587 (0) | 2020.01.16 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 디자인 패턴
- 이펙티브 자바
- JPA
- 디자인패턴
- kotest
- 객체지향
- 테라폼
- node.js
- 알고리즘
- 이팩티브 자바
- 정규표현식
- 클린 아키텍처
- 코테
- 프로그래머스
- Spring
- Spring Boot
- BOJ
- AWS
- BAEKJOON
- Effective Java
- 클린 코드
- Olympiad
- kkoon9
- Kotlin
- Algorithm
- MSA
- Java
- C++
- 백준
- programmers
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함
