Search

๊ฐ•๋™์—ฐ

2.๋ฌธ์ œ์ด๋ฆ„
3. ์ˆ˜ํ–‰์‹œ๊ฐ„[์ดˆ(s)]
255
์ข‹์•„์š” ๋ˆ„๋ฅด๊ธฐ
์ข‹์•„์š” ์ˆ˜
: 0
5 more properties
| ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Solution { public int[] solution(int[] answers) { int[] answer = {}; int[] stu1 = {1,2,3,4,5}; int[] stu2 = {2,1,2,3,2,4,2,5}; int[] stu3 = {3,3,1,1,2,2,4,4,5,5}; int[] cnt = {0,0,0}; for (int i = 0; i < answers.length; i++) { if (answers[i] == stu1[i % stu1.length]) { cnt[0] += 1; } if (answers[i] == stu2[i % stu2.length]) { cnt[1] += 1; } if (answers[i] == stu3[i % stu3.length]) { cnt[2] += 1; } } int max_cnt = (cnt[0]> cnt[1]) ? cnt[0] : cnt[1]; max_cnt = (max_cnt> cnt[2]) ? max_cnt : cnt[2]; List<Integer> arr = new ArrayList<>(); for (int i = 0; i <3; i++) { if (cnt[i] == max_cnt) arr.add(i+1); } answer = new int[arr.size()]; for (int i = 0 ; i < arr.size() ; i++) answer[i] = arr.get(i).intValue(); return answer; } }
Java
๋ณต์‚ฌ
def solution(answers): answer = [] stu1=[1,2,3,4,5] stu2=[2,1,2,3,2,4,2,5] stu3=[3,3,1,1,2,2,4,4,5,5] cnt=[0]*3 for i in range(len(answers)): if answers[i]==stu1[i%len(stu1)]: cnt[0]+=1 if answers[i]==stu2[i%len(stu2)]: cnt[1]+=1 if answers[i]==stu3[i%len(stu3)]: cnt[2]+=1 max_cnt=max(cnt) for i in range(len(cnt)): if cnt[i]==max_cnt: answer.append(i+1) return answer
Python
๋ณต์‚ฌ
| ์ฝ”๋“œ ์„ค๋ช…ํ•˜๊ธฐ
์ˆ˜์—…์‹œ๊ฐ„์— ํ’€์ดํ–ˆ๋˜ ๋ฌธ์ œ์—ฌ์„œ ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํŒŒ์ด์ฌ์œผ๋กœ ํ’€ ๋•Œ๋Š” ํ•œ ๋ฒˆ์— ์ญ‰ ํ’€์—ˆ๋Š”๋ฐ, ์ž๋ฐ”๋Š” ๊ฝค ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค..
1.
๊ฐ ํ•™์ƒ์˜ ์ฐ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•œ๋‹ค.
2.
๊ฐ ํ•™์ƒ์˜ ๋งž์€ ๋ฌธ์ œ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  cnt ๋ฐฐ์—ด์„ ํ•™์ƒ ์ˆ˜ ๋งŒํผ ์„ ์–ธํ•œ๋‹ค.
3.
๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„ answer ์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ํ•™์ƒ๋“ค์˜ ๋‹ต์ด ๋งž๋Š”์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ cnt๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
์ด ๋•Œ, ํ•™์ƒ ๋ฐฐ์—ด์€ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋ฅผ ํ•™์ƒ ์ˆ˜ ๋งŒํผ ๋‚˜๋ˆ„์–ด(๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉ)ํ•˜์—ฌ ๋น„๊ตํ•œ๋‹ค.
4.
cnt ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
5.
๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•™์ƒ์˜ ๋ฒˆํ˜ธ๋ฅผ answer ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.