2806. Account Balance After Rounded Purchase LeetCode Solution

In this guide, you will get 2806. Account Balance After Rounded Purchase LeetCode Solution with the best time and space complexity. The solution to Account Balance After Rounded Purchase problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Account Balance After Rounded Purchase solution in C++
  4. Account Balance After Rounded Purchase solution in Java
  5. Account Balance After Rounded Purchase solution in Python
  6. Additional Resources
2806. Account Balance After Rounded Purchase LeetCode Solution image

Problem Statement of 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, 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:

0 is considered to be a multiple of 10 in this problem.
When rounding, 5 is rounded upward (5 is rounded to 10, 15 is rounded to 20, 25 to 30, and so on).

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.

See also  3330. Find the Original Typed String I LeetCode Solution

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 <= 100

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2806. Account Balance After Rounded Purchase LeetCode Solution in C++

class Solution {
 public:
  int accountBalanceAfterPurchase(int purchaseAmount) {
    return 100 - ((purchaseAmount + 5) / 10) * 10;
  }
};
/* code provided by PROGIEZ */

2806. Account Balance After Rounded Purchase LeetCode Solution in Java

class Solution {
  public int accountBalanceAfterPurchase(int purchaseAmount) {
    return 100 - ((purchaseAmount + 5) / 10) * 10;
  }
}
// code provided by PROGIEZ

2806. Account Balance After Rounded Purchase LeetCode Solution in Python

class Solution:
  def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
    return 100 - ((purchaseAmount + 5) // 10) * 10
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.