#2806
Account Balance After Rounded Purchase
EasyMathMathInteger Manipulation
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
The optimal solution uses a mathematical formula to calculate the rounded amount directly, which is much more efficient than looping through multiples.
⚙️
Algorithm
3 steps- 1Step 1: Calculate the rounded amount using the formula: roundedAmount = ((purchaseAmount + 5) // 10) * 10.
- 2Step 2: Subtract the rounded amount from the initial balance of 100.
- 3Step 3: Return the final balance.
solution.py6 lines
1# Full working Python code
2balance = 100
3purchaseAmount = 9
4roundedAmount = ((purchaseAmount + 5) // 10) * 10
5final_balance = balance - roundedAmount
6print(final_balance)ℹ
Complexity note: Both time and space complexity are O(1) because we are performing a constant number of operations regardless of the input size.
- 1Rounding can be efficiently calculated using mathematical formulas.
- 2Understanding how to manipulate integers can simplify problems.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.