Search

๊น€๋„ํ˜„

2.๋ฌธ์ œ์ด๋ฆ„
3. ์ˆ˜ํ–‰์‹œ๊ฐ„[์ดˆ(s)]
900
์ข‹์•„์š” ๋ˆ„๋ฅด๊ธฐ
์ข‹์•„์š” ์ˆ˜
: 0
5 more properties
| ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
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.
๋ฐ˜ํ™˜ํ•  ๋ฐฐ์—ด์— ์ดˆ๊ธฐํ™” ์‹œ์ผœ ๋ฐ˜ํ™˜