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, in other words, its price.
When making the purchase, first the purchaseAmount is rounded to the nearest multiple of 10. Let us call this value roundedAmount. Then, roundedAmount dollars are removed from your bank account.
Return an integer denoting your final bank account balance after this purchase.
Notes:
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:
0 <= purchaseAmount <= 100This approach involves solving the problem using basic iterative methods. We explore the problem using straightforward loops and conditions, which leads to an easy-to-understand solution.
This C code iteratively prints the elements of the array. The exampleFunction iterates through the array using a for loop. Each element is accessed using the loop index and printed using the printf function.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), Space Complexity: O(1)
This approach solves the problem using recursion. Recursive methods break down the problem into smaller subproblems by calling the same function within itself.
This C recursive function continues calling itself with the next index until reaching the end of the array. Only one element is processed per call, leading to a sequence of recursive calls.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), Space Complexity: O(n) due to the call stack
| Approach | Complexity |
|---|---|
| Approach using Direct Iterative Methods | Time Complexity: O(n), Space Complexity: O(1) |
| Approach using Recursive Methods | Time Complexity: O(n), Space Complexity: O(n) due to the call stack |
The unfair way I got good at Leetcode • Dave Burji • 596,394 views views
Watch 9 more video solutions →Practice Account Balance After Rounded Purchase with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor