알고리즘 풀이/프로그래머스

[프로그래머스][Java] 모의고사

배게 2019. 2. 22. 16:54
728x90

학생클래스를 선언하여 학생의 번호와 받은 점수를 정의함


pattern을 2차원 배열에 저장하여


for문을 통해 학생들의 점수를 체크하고 Student객체를 정의함


Comparator 클래스를 통해 학생들의 점수에 내림차순으로 정렬함


동시최대정답자가 2~3명인 경우도 있으므로


그러한 경우 처리


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
63
64
import java.util.*;



class Solution {
    static class Student {
        int num;
        int point;
        Student(int num, int point){
            this.num = num;
            this.point = point;
        }

    }

    public int[] solution(int[] answers) {
        int[][] pattern = {
                {1,2,3,4,5},
                {2,1,2,3,2,4,2,5},
                {3,3,1,1,2,2,4,4,5,5}

        };

        int[] point = new int[3];
        List<Student> students = new ArrayList<Student>();

        for (int i = 0; i < answers.length; i++) {
            int ans = answers[i];
            if(ans==pattern[0][i%5]) point[0]++;
            if(ans==pattern[1][i%8]) point[1]++;
            if(ans==pattern[2][i%10]) point[2]++;
        }

        for (int i = 0; i < point.length; i++) {
            students.add(new Student(i+1, point[i]));
        }

        Comparator<Student> CompPoint = new Comparator<Student>() {

            @Override
            public int compare(Student a, Student b) {
                // TODO Auto-generated method stub
                return b.point-a.point;
            }
        };


        students.sort(CompPoint);

        int leng=1;
        if(students.get(0).point==students.get(1).point) leng++;
        if(students.get(0).point==students.get(2).point) leng++;

        int[] answer= new int[leng];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = students.get(i).num;
            System.out.println(students.get(i).point);
        }

        return answer;

    }
}