[LeetCode] Account Balance After Rounded Purchase

2806. Account Balance After Rounded Purchase

Initially, you have a bank account balance of 100 dollars.

You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars.

At the store where you will make the purchase, the purchase amount is rounded to the nearest multiple of 10. In other words, you pay a non-negative amount, roundedAmount, such that roundedAmount is a multiple of 10 and abs(roundedAmount - purchaseAmount) is minimized.

If there is more than one nearest multiple of 10, the largest multiple is chosen.

Return an integer denoting your account balance after making a purchase worth purchaseAmount dollars from the store.

Note: 0 is considered to be a multiple of 10 in this problem.

1
2
3
4
5
6
7
8
9
class Solution {
public:
int accountBalanceAfterPurchase(int purchaseAmount) {
if(purchaseAmount % 10 < 5) purchaseAmount = purchaseAmount - (purchaseAmount % 10);
else purchaseAmount = ((purchaseAmount / 10) + 1) * 10;
return 100 - purchaseAmount;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/06/PS/LeetCode/account-balance-after-rounded-purchase/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.