리트코드/easy
[리트코드] 2806. Account Balance After Rounded Purchase - js
bbeyak
2023. 9. 5. 11:16
반응형
1. 문제
https://leetcode.com/problems/account-balance-after-rounded-purchase/
Account Balance After Rounded Purchase - LeetCode
Can you solve this real interview question? 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
leetcode.com
2. 코드
이 문제는 두 가지 경우를 나뉘어 따져주면 된다.
- purchaseAmount 값의 나머지가 5보다 적을 때 = purchaseAmount에서 나머지를 빼서 10의 배수로 만들어준다.
- purchaseAmount 값의 나머지가 5보다 클 때 = purchaseAmount에서 (10 - 나머지)를 더해서 10의 배수로 만들어준다.
/**
* @param {number} purchaseAmount
* @return {number}
*/
var accountBalanceAfterPurchase = function(purchaseAmount) {
let rem = purchaseAmount % 10;
if (rem < 5)
purchaseAmount = purchaseAmount - rem;
else
purchaseAmount = purchaseAmount + (10 - rem);
return 100 - purchaseAmount;
};
반응형