ALGORITHM ๐Ÿค–/Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ชจ์˜๊ณ ์‚ฌ

daxx0ne 2023. 4. 26. 11:43

https://school.programmers.co.kr/learn/courses/30/lessons/42840

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] supoza1 = {1, 2, 3, 4, 5};
        int[] supoza2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] supoza3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int[] score = new int[3];
        int a = 1;
        int temp = 0;
        for (int k : answers) { // ์ˆ˜ํฌ์ž1 ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
            if (supoza1[temp] == k) {
                score[0] += a;
            }
            temp++;
            if (temp == 5) {
                temp = 0;
            }
        }
        temp = 0;
        for (int k : answers) { // ์ˆ˜ํฌ์ž2 ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
            if (supoza2[temp] == k) {
                score[1] += a;
            }
            temp++;
            if (temp == 8) {
                temp = 0;
            }
        }
        temp = 0;
        for (int k : answers) { // ์ˆ˜ํฌ์ž3 ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
            if (supoza3[temp] == k) {
                score[2] += a;
            }
            temp++;
            if (temp == 10) {
                temp = 0;
            }
        }
        int max = score[0];
        int high = 0;
        for (int j : score) { // ์ตœ๋Œ€ ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
            if (max < j) {
                max = j;
            }
        }
        for (int j : score) { // ๊ฐ€์žฅ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•˜๊ธฐ
            if (j == max) {
                high++;
            }
        }
        int[] answer = new int[high];
        int index = 0;
        for (int i = 0; i < score.length; i++) { // ๋ฐฐ์—ด์— ์ธ๋ฑ์Šค ์ฑ„์šฐ๊ธฐ
            if (score[i] == max) {
                answer[index] = i + 1;
                index++;
            }
        }
        Arrays.sort(answer); // ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
        return answer;
    }
}