Search

์—„์ˆœ๋ฏผ

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