
나의 풀이
class Solution {
public long solution(int price, int money, int count) {
long answer = 0;
for(int i = 1; i <= count ; i++){
answer += (price * i);
}
if(money < answer){
return answer - money;
}
return 0;
}
}
다른 사람의 풀이
class Solution {
public long solution(int price, int money, int count) {
long totalUsePrice = 0;
for (int i = 1; i <= count; i++) {
totalUsePrice += price * i;
}
long answer = totalUsePrice - money;
return answer > 0 ? answer : 0;
}
}
class Solution {
public long solution(long price, long money, long count) {
return Math.max(price * (count * (count + 1) / 2) - money, 0);
}
}
수학을 다시 공부해야하나..
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 직사각형 별찍기(Java) (0) | 2024.11.22 |
|---|---|
| [프로그래머스] 문자열 다루기 기본(Java) (0) | 2024.11.21 |
| [프로그래머스] 문자열 내림차순으로 배치하기(Java) (0) | 2024.11.18 |
| [프로그래머스] 약수의 개수와 덧셈(Java) (0) | 2024.11.17 |
| [프로그래머스] 내적(Java) (0) | 2024.11.16 |