KotlinAlgorithm#16 (POG 부족한 금액 계산하기)

박채빈·2023년 7월 15일
0

KotlinAlgorithm

목록 보기
16/28
post-thumbnail

POG 부족한 금액 계산하기

링크

코드

class Solution {
    fun solution(price: Int, money: Int, count: Int): Long {
        var loop = 0L
        (1..count).forEach { loop += it }
        val total = price * loop
        return if(money - total < 0) {
            total - money
        } else {
            0
        }
    }

    fun solution1(price: Int, money: Int, count: Int): Long
            = (1..count).sumOf { it * price.toLong() }.let { if (money > it) 0 else it - money }
}

Solution1 은 좀 변태같아서 가져와봄

profile
안드로이드 개발자

0개의 댓글