https://programmers.co.kr/learn/courses/30/lessons/42840?language=java
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ๊ณ ๋์ ์์ ํ์ ๋ชจ์๊ณ ์ฌ(์ํฌ์) ๋ฌธ์ ํ์ด์ด๋ค.
์ํฌ์๊ฐ 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();
}
}
'๊ฐ๋ฐ > ์๋ฃ๊ตฌ์กฐ & ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๊ณ ๋ฆฌ์ฆ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ด ๋ชจ์ (0) | 2022.07.08 |
---|---|
LeetCode - Missing Number (0) | 2022.05.30 |
K๋ฒ์งธ์ (Java) (0) | 2022.02.09 |
์ฒด์ก๋ณต (Java) (0) | 2022.02.08 |
์ฃผ์ ๊ฐ๊ฒฉ (Java) (0) | 2022.02.06 |
๋๊ธ