|
์ฝ๋ ์์ฑํ๊ธฐ
public int[] solution(int[] answer) {
// ์ฐ์ ๋ต์ง.
int[] first = {1, 2, 3, 4, 5};
int[] second = {2, 1, 2, 3, 2, 4, 2, 5};
int[] third = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
// ๋ช๊ฐ ๋ง์ท๋์ง ์ฒดํฌํ๋ ๋ฐฐ์ด
int[] cnt = {0, 0, 0};
// ์ฐ์ ๋ต์ง๋ฅผ ์ํ์ํค๋ฉฐ ์ง์ง ๋ต๊ณผ ๋น๊ต.
// % ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฐ์ ๋ต์ง๋ฅผ ์ํ ์ํจ๋ค.
for (int i = 0; i < answer.length; i++) {
if (answer[i] == first[i%first.length]){
cnt[0]++;
}
if (answer[i] == second[i%second.length]){
cnt[1]++;
}
if (answer[i] == third[i%third.length]){
cnt[2]++;
}
}
// ๋ง์ถ ๊ฐ์์ ์ต๋๊ฐ ๊ตฌํ๊ธฐ
int max = 0;
for (int i = 0; i < cnt.length; i++) {
if(cnt[i] > max) {
max = cnt[i];
}
}
// ์ต๋๊ฐ์ ์ด์ฉํ์ฌ, 1๋ฑํ ์ฌ๋์ ๋ฆฌ์คํธ์ ์ถ๊ฐ
List<Integer> result = new ArrayList<>();
if(cnt[0] == max ) result.add(1);
if(cnt[1] == max ) result.add(2);
if(cnt[2] == max ) result.add(3);
// ๋ฆฌ์คํธ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ๊พธ๊ธฐ
int[] winner = new int[result.size()];
for (int i = 0; i < winner.length; i++) {
winner[i] = result.get(i);
}
return winner;
}
Java
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
1.
% ์ฐ์ฐ์ ์ฌ์ฉํ์ฌ ์ฐ์ ๋ต์ง๋ฅผ ์ํ์์ผ ์ง์ง ๋ต์ง์ ๋น๊ตํ๋ค.
2.
๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด 1๋ฑํ ์ฌ๋์ ๊ตฌํ๊ณ , ๊ทธ ๋ฆฌ์คํธ์ size()๋ฅผ ์ด์ฉํด returnํ ๋ฐฐ์ด์ length๋ฅผ ๊ฒฐ์ ํ๋ค.