[프로그래머스] 부족한 금액 계산하기

Zoo Da·2021년 9월 29일
0

프로그래머스

목록 보기
3/10
post-thumbnail

링크

https://programmers.co.kr/learn/courses/30/lessons/82612

sol1 (O(n))

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

long long solution(int price, int money, int count)
{
    ll answer = -1;
    ll temp = 0;
    for(ll i = 1; i <= count; i++) temp += (i * price);
    answer = abs(temp - money);
    return (money > temp ? 0 : answer);
}

ps) 가우스 공식 n(n + 1) / 2를 이용해도 됩니다 이렇게되면 O(1)의 시간에 해결할 수 있습니다.

profile
메모장 겸 블로그

0개의 댓글