[Programmers] 모의고사 - JAVA

ONE·2022년 2월 9일
0

Programmers

목록 보기
5/24

📚 Problem

모의고사

  • 수포자 3명이 문제를 각각 정해진 배열의 순서대로 찍음
  • 가장 많은 문제를 맞힌 사람을 찾기
  • 맞힌 문제수가 같다면 번호 값을 오름차순 정렬

📝 Solution

Key Idea

  • 학생을 클래스로 우선순위 큐에서 사용할 수 있도록 구현
  • 맞힌수의 내림차순으로 정렬되게하고 맞힌 수가 같다면 번호로 오름차순 정렬합니다
class GiveUpMath implements Comparable<GiveUpMath> {
    private int num;
    private int count;
    private int[] solvingAry;

    public GiveUpMath(int num, int[] solvingAry) {
        this.num = num;
        this.solvingAry = solvingAry;
        count = 0;
    }

    public int getNum() {
        return this.num;
    }

    public int getCount() {
        return this.count;
    }

    public int[] getSolvingAry() {
        return this.solvingAry;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public int compareTo(GiveUpMath o) {
        if(count == o.count)
            return num - o.num;
        return o.count - count;
    }
}

💻 Code

Solution.java

import java.util.ArrayList;
import java.util.PriorityQueue;

class GiveUpMath implements Comparable<GiveUpMath> {
    private int num;
    private int count;
    private int[] solvingAry;

    public GiveUpMath(int num, int[] solvingAry) {
        this.num = num;
        this.solvingAry = solvingAry;
        count = 0;
    }

    public int getNum() {
        return this.num;
    }

    public int getCount() {
        return this.count;
    }

    public int[] getSolvingAry() {
        return this.solvingAry;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public int compareTo(GiveUpMath o) {
        if(count == o.count)
            return num - o.num;
        return o.count - count;
    }
}

class Solution {
    public int[] solution(int[] answers) {
        ArrayList<Integer> answer = new ArrayList<>();
        GiveUpMath[] array = initArray();
        PriorityQueue<GiveUpMath> queue = new PriorityQueue<>();

        for (GiveUpMath std : array){
            std.setCount(grading(std, answers));
            queue.add(std);
        }

        GiveUpMath first = queue.poll();
        answer.add(first.getNum());

        while (!queue.isEmpty()) {
            GiveUpMath current = queue.poll();
            if(current.getCount() == first.getCount())
                answer.add(current.getNum());
        }

        int[] result = new int[answer.size()];
        for(int i = 0; i < result.length; i++)
            result[i] = answer.get(i);

        return result;
    }

    private GiveUpMath[] initArray () {
        GiveUpMath[] ary = new GiveUpMath[3];

        ary[0] = new GiveUpMath(1, new int[]{1, 2, 3, 4, 5});
        ary[1] = new GiveUpMath(2, new int[]{2, 1, 2, 3, 2, 4, 2, 5});
        ary[2] = new GiveUpMath(3, new int[]{3, 3, 1, 1, 2, 2, 4, 4, 5, 5});

        return ary;
    }

    private int grading(GiveUpMath std, int[] answers) {
        int count  = 0;
        for (int i = 0; i < answers.length; i++)
            if(answers[i] == std.getSolvingAry()[i % std.getSolvingAry().length])
                count++;
        return count;
    }
}

SolutionTest.java

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class SolutionTest {
    Solution solution;

    @BeforeEach
    public void setSol(){
        solution = new Solution();
    }

    @Test
    public void solution_1(){
        int[] result = solution.solution(new int[]{1, 2, 3, 4, 5});
        assertArrayEquals(new int[]{1}, result);
    }

    @Test
    public void solution_2(){
        int[] result = solution.solution(new int[]{1, 3, 2, 4, 2});
        assertArrayEquals(new int[]{1, 2, 3}, result);
    }
}
profile
New, Strange, Want to try

0개의 댓글