import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public int[] solution(int[] answers) {
int[] stu1 = { 1, 2, 3, 4, 5 };
int[] stu2 = { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] stu3 = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int stu1idx = 0, stu2idx = 0, stu3idx = 0;
ArrayList<Integer> score = new ArrayList<>();
ArrayList<Integer> answerls = new ArrayList<>();
score.add(0);
score.add(0);
score.add(0);
for (int i = 0; i < answers.length; i++) {
if (stu1idx > stu1.length - 1) {
stu1idx = 0;
}
if (answers[i] == stu1[stu1idx]) {
score.set(0, score.get(0) + 1);
}
if (stu2idx > stu2.length - 1) {
stu2idx = 0;
}
if (answers[i] == stu2[stu2idx]) {
score.set(1, score.get(1) + 1);
}
if (stu3idx > stu3.length - 1) {
stu3idx = 0;
}
if (answers[i] == stu3[stu3idx]) {
score.set(2, score.get(2) + 1);
}
stu1idx++;
stu2idx++;
stu3idx++;
}
int max = Collections.max(score);
for (int i = 0; i < 3; i++) {
if (max == score.get(i)) {
answerls.add(i + 1);
}
}
int[] answer = new int[answerls.size()];
for (int i = 0; i < answerls.size(); i++) {
answer[i] = answerls.get(i);
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
int[] answers = { 1, 3, 2, 4, 2 };
System.out.println(s.solution(answers).toString());
}
}
알고리즘 설명 : 답안배열과 학생들의 찍기패턴배열의 idx가 다름으로 학생별 별도의 idx를 사용. 1개의 패턴이 끝날시 반복하도록 진행하였다.
그 후 LIST를 이용하여 학생들의 점수를 채점하고 MAX값을 구해 별도의 순위 LIST를 구현하고 반환을 위한 answer 배열에 삽입후 리턴.
크기가 다른 배열을 순회해야할때 별도의 idx를 사용하는것과 크기비교가 3개 이상일때 Collections 기능을 사용하는걸 숙지.
코드보완 부분 : 별도의 idx를 이용하지않고 나머지값을 이용함으로 조금 더 깔끔한 코드가 완성됄수 있었음.
for(int i = 0; i < answers.length; i++){
if(answers[i] == a[(i+1)%5]) arr[0]++;
if(answers[i] == b[(i+1)%8]) arr[1]++;
if(answers[i] == c[(i+1)%10]) arr[2]++;
}
답안 :
SELECT USER_ID , PRODUCT_ID
FROM ONLINE_SALE
GROUP BY 1, 2
HAVING COUNT(1) >= 2
ORDER BY 1, 2 DESC
답안 :
SELECT MAX(DATETIME)
from ANIMAL_INS
답안 :
SELECT u2.USER_ID, u2.NICKNAME, concat(CITY,' ',STREET_ADDRESS1,' ',STREET_ADDRESS2) as '전체주소', CONCAT(substr(TLNO,1,3), '-', substr(TLNO,4,4), '-', substr(TLNO,8,4)) as '전화번호'
from USED_GOODS_BOARD u1
left join USED_GOODS_USER u2
on u1.WRITER_ID = u2.USER_ID
group by 1
HAVING count(1) >=3
order by 1 desc