728x90
def solution(answers):
answer = []
answer_temp = []
count01 = 0
count02 = 0
count03 = 0
one = [1,2,3,4,5]
two = [2,1,2,3,2,4,2,5]
three = [3,3,1,1,2,2,4,4,5,5]
for i in range(len(answers)):
if answers[i] == one[i%len(one)]:
count01+=1
if answers[i] == two[i%len(two)]:
count02+=1
if answers[i] == three[i%len(three)]:
count03+=1
answer_temp = [count01, count02, count03]
for person, score in enumerate(answer_temp):
if score == max(answer_temp):
answer.append(person+1)
return answer
more concise one:
def solution(answers):
p = [[1, 2, 3, 4, 5],
[2, 1, 2, 3, 2, 4, 2, 5],
[3, 3, 1, 1, 2, 2, 4, 4, 5, 5]]
s = [0] * len(p)
for q, a in enumerate(answers):
for i, v in enumerate(p):
if a == v[q % len(v)]:
s[i] += 1
return [i + 1 for i, v in enumerate(s) if v == max(s)]
Javascript
let answers = [1,3,2,4,2]
function solution(answers) {
let soopo1=[1, 2, 3, 4, 5];
let soopo2=[2, 1, 2, 3, 2, 4, 2, 5];
let soopo3=[3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let count=[0, 0, 0];
for(let i = 0; i<answers.length; i++){
if(soopo1[i % soopo1.length] === answers[i]){
count[0]++;
};
if(soopo2[i % soopo2.length] === answers[i]){
count[1]++;
};
if(soopo3[i % soopo3.length] === answers[i]){
count[2]++;
};
};
let max = Math.max(...count);
let result=[];
for(let j = 0; j < count.length; j++){
if(count[j] === max){
result.push(j+1);
};
};
return result;
};
solution(answers);
- Math.max() 함수는 입력된 숫자 중 가장 큰 숫자를 반환
- spread operator, i.e. "..." 이 함수를 사용하면 배열의 숫자들 중 가장 큰 숫자를 쉽게 얻을 수 있습니다.
var arr = [1, 2, 3];
var max = Math.max(...arr);
ref:
728x90
'Algorithm' 카테고리의 다른 글
[프로그래머스] 2016년 (Python, JS) (0) | 2021.01.25 |
---|---|
[프로그래머스] 두 개 뽑아서 더하기 (JS, Python) (0) | 2021.01.25 |
[프로그래머스] k번째 수 (JS, Python) (0) | 2021.01.24 |
[프로그래머스] 체육복 (javascript) (0) | 2021.01.23 |
[프로그래머스] 체육복 (python) (0) | 2021.01.18 |
댓글