프로그래머스 - JAVA
코딩테스트 연습 > 완전탐색 > 모의고사
CHO_is
2021. 6. 13. 12:44
이전 코드
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] answer = {};
ArrayList <Student> stArrayList = new ArrayList<>();
stArrayList.add(new Student(1));
stArrayList.add(new Student(2));
stArrayList.add(new Student(3));
Queue<Integer> secondAns = new LinkedList<>(Arrays.asList(2, 1, 2, 3, 2, 4, 2, 5));
Queue<Integer> thirdAns = new LinkedList<>(Arrays.asList(3, 3, 1, 1, 2, 2, 4, 4, 5, 5));
for (int i = 0, ans = 1 ; i < answers.length ; i ++, ans++){
if (ans > 5){
ans = 1;
}
if(ans == answers[i]){
stArrayList.get(0).upScore();
}
if(secondAns.peek() == answers[i]){
stArrayList.get(1).upScore();
}
if(thirdAns.peek() == answers[i]){
stArrayList.get(2).upScore();
}
secondAns.add(secondAns.poll());
thirdAns.add(thirdAns.poll());
}
Collections.sort(stArrayList);
if(stArrayList.get(0).getScore() != stArrayList.get(1).getScore()){
return new int[]{stArrayList.get(0).getId()};
} else{
if(stArrayList.get(1).getScore() != stArrayList.get(2).getScore()){
int [] array = {stArrayList.get(0).getId(), stArrayList.get(1).getId()};
return array;
} else{
return new int[]{1, 2, 3};
}
}
}
}
class Student implements Comparable<Student> {
int id;
int score;
public Student(int id){
this.id = id;
score = 0;
}
public int getId(){
return this.id;
}
public int getScore(){
return this.score;
}
public void upScore(){
this.score ++;
}
@Override
public int compareTo(Student o) {
if (o.getScore() == this.getScore()) {
return this.getId() - o.getId();
} else {
return o.getScore() - this.getScore();
}
}
}
최근 코드
import java.util.*;
class Solution {
public Integer[] solution(int[] answers) {
int [] correctCount = new int [3];
int [] student1 = {1, 2, 3, 4, 5};
int [] student2 = {2, 1, 2, 3, 2, 4, 2, 5};
int [] student3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int answer : answers){
if (answer == student1[count1]){
correctCount[0] ++;
}
if (answer == student2[count2]){
correctCount[1] ++;
}
if (answer == student3[count3]){
correctCount[2] ++;
}
if (count1 == student1.length - 1){
count1 = 0;
} else {
count1 ++;
}
if (count2 == student2.length - 1){
count2 = 0;
} else {
count2 ++;
}
if (count3 == student3.length - 1){
count3 = 0;
} else {
count3 ++;
}
}
int [] correctCountClone = correctCount.clone();
Arrays.sort(correctCountClone);
ArrayList <Integer> ansArrayList = new ArrayList<>();
for (int i = 0 ; i < correctCount.length ; i++){
if (correctCountClone[correctCountClone.length - 1] == correctCount[i]){
ansArrayList.add(i + 1);
}
}
return ansArrayList.toArray(new Integer[ansArrayList.size()]);
}
}