[programmers] ์ˆ˜ํฌ์ž - Java

"์ˆ˜ํฌ์ž" ๋ฌธ์ œ๋Š” ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ฐ๋Š” 3๋ช…์˜ ์ˆ˜ํฌ์ž ์ค‘ ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์„ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ˆ˜ํฌ์ž์˜ ์ฐ๋Š” ํŒจํ„ด์ด ์ฃผ์–ด์ง€๊ณ , ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ๋‹ต์•ˆ ๋ฐฐ์—ด๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ์–ป์€ ์ˆ˜ํฌ์ž๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ •๋‹ต์„ ๋ฐ˜๋ณตํ•ด์„œ ๋น„๊ตํ•˜๊ณ , ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ˆ˜ํฌ์ž๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
DriedPollack's avatar
Mar 18, 2024
[programmers] ์ˆ˜ํฌ์ž - Java

์ˆ˜ํฌ์ž

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.
1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

answers
return
[1,2,3,4,5]
[1]
[1,3,2,4,2]
[1,2,3]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
  • ์ˆ˜ํฌ์ž 1์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ๋งžํ˜”์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 2๋Š” ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 3์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ๋งžํžŒ ์‚ฌ๋žŒ์€ ์ˆ˜ํฌ์ž 1์ž…๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #2
  • ๋ชจ๋“  ์‚ฌ๋žŒ์ด 2๋ฌธ์ œ์”ฉ์„ ๋งž์ท„์Šต๋‹ˆ๋‹ค.

solution.java

import java.util.*; class Solution { public int[] solution(int[] answers) { int[] supoja1 = {1, 2, 3, 4, 5}; int[] supoja2 = {2, 1, 2, 3, 2, 4, 2, 5}; int[] supoja3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; int[] scores = new int[3]; for (int i = 0; i < answers.length; i++) { if (answers[i] == supoja1[i % supoja1.length]) scores[0]++; if (answers[i] == supoja2[i % supoja2.length]) scores[1]++; if (answers[i] == supoja3[i % supoja3.length]) scores[2]++; } int maxScore = Math.max(scores[0], Math.max(scores[1], scores[2])); List<Integer> resultList = new ArrayList<>(); for (int i = 0; i < scores.length; i++) { if (scores[i] == maxScore) { resultList.add(i + 1); } } int[] result = new int[resultList.size()]; for (int i = 0; i < resultList.size(); i++) { result[i] = resultList.get(i); } return result; } }
 

ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ

  • ์ˆ˜ํฌ์ž 3๋ช…์˜ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.
  • answers ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์ˆ˜ํฌ์ž 3๋ช…์˜ ๋‹ต์•ˆ๊ณผ ์ •๋‹ต์ด ์ผ์น˜ํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ++ํ•œ๋‹ค.
  • ์ˆ˜ํฌ์ž 3๋ช…์ค‘ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ์ฐพ๊ณ , ํ•ด๋‹น ์ ์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š” ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ๋ฆฌํ„ดํ•œ๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์–ด๋–ป๊ฒŒ ์ •๋‹ต๊ณผ 3๊ฐœ์˜ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ, ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ํ†ตํ•ด์„œ ์ •๋‹ต์„ ๋ฐ˜๋ณตํ•ด์„œ ๋น„๊ตํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 3์žฅ ์ •๋ฆฌ

DriedPollack's avatar
March 18, 2024
[์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ์ •ํŒ] 3์žฅ ์ •๋ฆฌ

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ปDriedPollack's Blog