https://programmers.co.kr/learn/courses/30/lessons/42840
1. 정답 배열이 들어오기때문에 사용자의 입력리스트로 나눠서 일치여부를 합산하면 된다.
2. 사용자의 구분과 배열 정보를 갖고 있어야하기때문에 class로 사용자구분, 정답지, 맞은 갯수를 갖게한다.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Student {
private int id;
private int match;
private int[] submitAnswers = {};
public Student(int id, int[] submitAnswers, int[] answers) {
this.id = id;
this.submitAnswers = submitAnswers;
this.match = checkMatches(answers);
}
private int checkMatches(int[] answers) {
int temp = 0;
for (int i = 0; i < answers.length; i++) {
if (answers[i] == submitAnswers[i % submitAnswers.length]) {
temp++;
}
}
return temp;
}
public int getId() {
return id;
}
public int getMatch() {
return match;
}
}
class Solution {
private static int[] student1Submits = {1, 2, 3, 4, 5};
private static int[] student2Submits = {2, 1, 2, 3, 2, 4, 2, 5};
private static int[] student3Submits = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
public int[] solution(int[] answers) {
List<Integer> answer = new ArrayList<>();
List<Student> students = Arrays.asList(
new Student(1, student1Submits, answers),
new Student(2, student2Submits, answers),
new Student(3, student3Submits, answers)
);
int max = students.stream()
.map(Student::getMatch)
.max(Integer::compare)
.get();
return students.stream()
.filter(student -> student.getMatch() == max)
.map(Student::getId)
.mapToInt(Integer::intValue)
.toArray();
}
}
|
cs |