Difficulty: Easy | Acceptance: 56.40% | Paid: No Topics: Math
Initially, you have a bank account balance of 100 dollars.
You are given an integer purchaseAmount representing the amount you will spend on a single purchase in dollars.
At the store where you will make the purchase, the amount is rounded to the nearest multiple of 10. If the number ends in a 5 or greater, it is rounded up. If it ends in 4 or less, it is rounded down.
Return an integer representing the balance that remains in your bank account after your purchase.
- Examples
- Constraints
- Mathematical Rounding
- Modulo Arithmetic
- String Manipulation
Examples
Example 1
Input:
purchaseAmount = 9
Output:
90
Explanation: The nearest multiple of 10 to 9 is 10. So your account balance becomes 100 - 10 = 90.
Example 2
Input:
purchaseAmount = 15
Output:
80
Explanation: The nearest multiple of 10 to 15 is 20. So your account balance becomes 100 - 20 = 80.
Example 3
Input:
purchaseAmount = 10
Output:
90
Explanation: 10 is a multiple of 10 itself. So your account balance becomes 100 - 10 = 90.
Constraints
1 <= purchaseAmount <= 100
Mathematical Rounding
Intuition Use integer arithmetic to round the purchase amount to the nearest multiple of 10 by adding 5 and then dividing by 10.
Steps
- Add 5 to the purchase amount
- Perform integer division by 10
- Multiply by 10 to get the rounded amount
- Subtract from 100 to get the remaining balance
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
rounded = ((purchaseAmount + 5) // 10) * 10
return 100 - rounded
Complexity
- Time: O(1)
- Space: O(1)
- Notes: Most efficient approach with constant time operations
Modulo Arithmetic
Intuition Extract the last digit using modulo operation and apply rounding rules based on whether it’s 5 or greater.
Steps
- Get the last digit using purchaseAmount % 10
- If last digit is 5 or more, round up by adding (10 - last digit)
- If last digit is less than 5, round down by subtracting last digit
- Return 100 minus the rounded amount
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
last_digit = purchaseAmount % 10
if last_digit >= 5:
rounded = purchaseAmount + (10 - last_digit)
else:
rounded = purchaseAmount - last_digit
return 100 - rounded
Complexity
- Time: O(1)
- Space: O(1)
- Notes: Slightly more verbose but equally efficient
String Manipulation
Intuition Convert the number to a string to extract the last digit and apply rounding logic.
Steps
- Convert purchaseAmount to string
- Extract the last character and convert back to integer
- Apply rounding rules based on the last digit
- Return 100 minus the rounded amount
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
s = str(purchaseAmount)
last_digit = int(s[-1])
if last_digit >= 5:
rounded = purchaseAmount + (10 - last_digit)
else:
rounded = purchaseAmount - last_digit
return 100 - rounded
Complexity
- Time: O(1) - String operations are constant time for fixed-size integers
- Space: O(1) - String representation is bounded by input size
- Notes: Less efficient due to string conversion overhead