[PRO][Lv.1] 부족한 금액 계산하기

김정현·2022년 12월 20일
0

프로그래머스

목록 보기
20/50

📚 Problem

부족한 금액 계산하기

💫 Solve

import Foundation

func solution(_ price:Int, _ money:Int, _ count:Int) -> Int64{
    var answer:Int64 = 0
    
    for i in 1...count {
        answer += Int64(i * price)
    }
    if answer - Int64(money) <= 0 {
        return 0
    }
    return answer - Int64(money)
}

🩺 Another Solution

import Foundation

func solution(_ price:Int, _ money:Int, _ count:Int) -> Int64{
    let totalPrice = price * (count * (count+1)/2)

    if money >= totalPrice {
        return 0
    }

    return Int64(totalPrice - money)
}
profile
🍎💻👍

0개의 댓글