|
์ฝ๋ ์์ฑํ๊ธฐ
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 ๋ฐฐ์ด์ ๋ด์ ๋ฐํํ๋ค.