๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชจ์˜๊ณ ์‚ฌ (Java)

by 1mj 2022. 2. 15.

https://programmers.co.kr/learn/courses/30/lessons/42840?language=java 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ์˜๊ณ ์‚ฌ

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š”

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  ์™„์ „ํƒ์ƒ‰ ๋ชจ์˜๊ณ ์‚ฌ(์ˆ˜ํฌ์ž) ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

์ˆ˜ํฌ์ž๊ฐ€ 3๋ช…์œผ๋กœ ์ •ํ•ด์ ธ ์žˆ๊ณ , ๊ฐ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๊ทœ์น™์„ ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•˜๊ณ  ๋ฌธ์ œ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์ฃผ์—ˆ๋‹ค.

 

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public int[] solution(int[] answers) {
        int[] route1 = {1, 2, 3, 4, 5};
        int[] route2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] route3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int[] scores = {0, 0, 0};
        
        for (int i = 0; i < answers.length; i++) {
            int answer = answers[i];
            if (route1[i % route1.length] == answer)    scores[0]++;
            if (route2[i % route2.length] == answer)    scores[1]++;
            if (route3[i % route3.length] == answer)    scores[2]++;
        }
        
        int topScore = Arrays.stream(scores).max().getAsInt();
        List<Integer> answer = new ArrayList<>();
        if (topScore == scores[0])  answer.add(1);
        if (topScore == scores[1])  answer.add(2);
        if (topScore == scores[2])  answer.add(3);
        return answer.stream().mapToInt(i -> i).toArray();
    }
}

 

๋Œ“๊ธ€