프로그래머스 LEVEL 1(부족한 금액 계산하기)

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2022-08-26

  • 해결 방법 :

    • n번째 마다 price에 곱하여 더한 후 total과 비교
  • 회고 : 등차 수열인 점 생각하기! -> 총합 = n * (n + 1) / 2

  • 코드

       function solution(price, money, count) {
        let total = 0;
        for (let i = 1; i <= count; i++) {
            total += price * i;
        }
        return total < money ? 0 : total - money;
    }
    
  • 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges