2303. Calculate Amount Paid in Taxes LeetCode Solution

In this guide, you will get 2303. Calculate Amount Paid in Taxes LeetCode Solution with the best time and space complexity. The solution to Calculate Amount Paid in Taxes 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. Calculate Amount Paid in Taxes solution in C++
  4. Calculate Amount Paid in Taxes solution in Java
  5. Calculate Amount Paid in Taxes solution in Python
  6. Additional Resources
2303. Calculate Amount Paid in Taxes LeetCode Solution image

Problem Statement of Calculate Amount Paid in Taxes

You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound (i.e. upperi-1 < upperi for 0 < i < brackets.length).
Tax is calculated as follows:

The first upper0 dollars earned are taxed at a rate of percent0.
The next upper1 – upper0 dollars earned are taxed at a rate of percent1.
The next upper2 – upper1 dollars earned are taxed at a rate of percent2.
And so on.

You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes. Answers within 10-5 of the actual answer will be accepted.

Example 1:

Input: brackets = [[3,50],[7,10],[12,25]], income = 10
Output: 2.65000
Explanation:
Based on your income, you have 3 dollars in the 1st tax bracket, 4 dollars in the 2nd tax bracket, and 3 dollars in the 3rd tax bracket.
The tax rate for the three tax brackets is 50%, 10%, and 25%, respectively.
In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.

See also  3341. Find Minimum Time to Reach Last Room I LeetCode Solution

Example 2:

Input: brackets = [[1,0],[4,25],[5,50]], income = 2
Output: 0.25000
Explanation:
Based on your income, you have 1 dollar in the 1st tax bracket and 1 dollar in the 2nd tax bracket.
The tax rate for the two tax brackets is 0% and 25%, respectively.
In total, you pay $1 * 0% + $1 * 25% = $0.25 in taxes.

Example 3:

Input: brackets = [[2,50]], income = 0
Output: 0.00000
Explanation:
You have no income to tax, so you have to pay a total of $0 in taxes.

Constraints:

1 <= brackets.length <= 100
1 <= upperi <= 1000
0 <= percenti <= 100
0 <= income <= 1000
upperi is sorted in ascending order.
All the values of upperi are unique.
The upper bound of the last tax bracket is greater than or equal to income.

Complexity Analysis

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

2303. Calculate Amount Paid in Taxes LeetCode Solution in C++

class Solution {
 public:
  double calculateTax(vector<vector<int>>& brackets, int income) {
    double ans = 0;
    int prev = 0;

    for (const vector<int>& b : brackets) {
      const int upper = b[0];
      const int percent = b[1];
      if (income < upper)
        return ans + (income - prev) * percent / 100.0;
      ans += (upper - prev) * percent / 100.0;
      prev = upper;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2303. Calculate Amount Paid in Taxes LeetCode Solution in Java

class Solution {
  public double calculateTax(int[][] brackets, int income) {
    double ans = 0;
    int prev = 0;

    for (int[] b : brackets) {
      final int upper = b[0];
      final int percent = b[1];
      if (income < upper)
        return ans + (income - prev) * percent / 100.0;
      ans += (upper - prev) * percent / 100.0;
      prev = upper;
    }

    return ans;
  }
}
// code provided by PROGIEZ

2303. Calculate Amount Paid in Taxes LeetCode Solution in Python

class Solution:
  def calculateTax(self, brackets: list[list[int]], income: int) -> float:
    ans = 0
    prev = 0

    for upper, percent in brackets:
      if income < upper:
        return ans + (income - prev) * percent / 100.0
      ans += (upper - prev) * percent / 100.0
      prev = upper

    return ans
# code by PROGIEZ

Additional Resources

See also  1789. Primary Department for Each Employee LeetCode Solution

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