본문 바로가기
Programmers/Lv.0

[프로그래머스][Js] Lv.0 - 연속된 수의 합

by pin9___9 2023. 3. 14.
728x90

문제💭


 

나의 풀이👨‍💻

function solution(num, total) {
  let answer = [];
  let n = Math.floor(total / num);
  for (let i = Math.round(-num / 2); i < Math.round(num / 2); i++) {
    num % 2 === 0 ? answer.push(n + 1 + i) : answer.push(n + i);
  }
  return answer;
}

 


다른 사람의 풀이👨‍🏫

1. 

function solution(num, total) {
    var min = Math.ceil(total/num - Math.floor(num/2));
    var max = Math.floor(total/num + Math.floor(num/2));

    return new Array(max-min+1).fill(0).map((el,i)=>{return i+min;});
}

2. 

function solution(num, total) {
    const a = (2 * total / num + 1 - num) / 2
    return Array(num).fill().map((_, i) => i + a)
}

 


Reference

  • 프로그래머스
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

728x90

댓글