|
์ฝ๋ ์์ฑํ๊ธฐ
import java.util.ArrayList;
import java.util.Arrays;
public class Programmers_brute_force {
public static int[] solution(int[] answers) {
int[] cnt = {0, 0, 0};
// 1.
int[] one = {1, 2, 3, 4, 5};
int[] two = {2, 1, 2, 3, 2, 4, 2, 5};
int[] three = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
// 2.
for (int i = 0; i < answers.length; i++) {
if (answers[i] == one[i % 5])
cnt[0]++;
if (answers[i] == two[i % 8])
cnt[1]++;
if (answers[i] == three[i % 10])
cnt[2]++;
}
// 3.
int max = Math.max(Math.max(cnt[0], cnt[1]), cnt[2]);
// 4.
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < cnt.length; i++) {
if(max == cnt[i])
list.add(i+1);
}
// 5.
int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
return answer;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(solution(new int[]{1,2,3,4,5}))); // 1
System.out.println(Arrays.toString(solution(new int[]{1,3,2,4,2}))); // 1, 2, 3
}
}
Java
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
๊ฐ์ ์๊ฐ์ ํ์๋ ๋ฌธ์ ๋ผ ๋นจ๋ฆฌ ํ ์ ์์์ต๋๋ค.
1.
1, 2, 3 ๋ฒ ์ํฌ์์ ์ฐ๋ ๋ฐฉ์์ ํจํด์ ๋ฐฐ์ด์ ์ ์ฅํ๊ณ
์ฃผ์ด์ง ๋ต์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋ฌธ์ ํตํด ์ํฌ์์ ๋ต๊ณผ ๋น๊ต ํ์ธ
2.
์ํฌ์ ์ฐ๋ ๋ฐฉ์ ํจํด ๋ฐฐ์ด % ๋ฐฐ์ด์ ๊ธธ์ด ์ ํ์ฌ ์์๋๋ก ์ ๋ต๊ณผ ๋น๊ตํ๊ณ ๋ง์ผ๋ฉด cnt ๋ฅผ ์ฆ๊ฐ
3.
์ ๋ต์ ๊ฐ์ฅ ๋ง์ด ๋ง์ถ ์ํฌ์๋ฅผ ์ฐพ๋๋ค.
4.
๊ฐ์ฅ ๋ง์ด ๋ง์ถ ์ํฌ์ ์๋งํผ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ์ ์ฅ
5.
๋ฐํํ ๋ฐฐ์ด์ ์ด๊ธฐํ ์์ผ ๋ฐํ