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:
upper0 dollars earned are taxed at a rate of percent0.upper1 - upper0 dollars earned are taxed at a rate of percent1.upper2 - upper1 dollars earned are taxed at a rate of percent2.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.
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 <= 1001 <= upperi <= 10000 <= percenti <= 1000 <= income <= 1000upperi is sorted in ascending order.upperi are unique.income.Problem Overview: You are given progressive tax brackets where each bracket defines an upper income limit and the percentage tax applied to that portion of income. The task is to compute the total tax paid for a given income by applying each bracket sequentially until the income limit is reached.
Approach 1: Iterative Calculation of Taxes by Bracket (O(n) time, O(1) space)
This approach directly simulates how progressive taxes are calculated in real systems. Iterate through the brackets array and determine how much of your income falls inside the current bracket. For each bracket, compute the taxable portion as min(income, upperLimit) - previousLimit, multiply it by the tax percentage, and accumulate the result. Update the previous limit as you move to the next bracket. Once the income falls below the current bracket's upper limit, the remaining tax can be computed and the loop stops. This method uses only simple arithmetic and a single pass through the array, making it ideal for problems involving array traversal and direct simulation.
Approach 2: Cumulative Summation with Early Exit (O(n) time, O(1) space)
This variation also processes brackets sequentially but focuses on accumulating the taxable income incrementally. Instead of recalculating limits repeatedly, track the remaining income and process each bracket's capacity. For each bracket, determine how much income can be taxed inside that range and add the computed tax to a running total. If the remaining income becomes zero or negative, break early since no further brackets apply. The early exit avoids unnecessary iterations when income lies within lower brackets. The algorithm still runs in linear time relative to the number of brackets and requires constant extra memory.
Recommended for interviews: The iterative bracket simulation is the expected solution. Interviewers want to see that you correctly model progressive tax calculation and handle bracket boundaries without off‑by‑one mistakes. Both implementations run in O(n) time with O(1) space, but the direct bracket iteration clearly demonstrates your understanding of range processing and controlled iteration over arrays.
This approach involves iterating through each tax bracket and calculating the tax for each, based on the remaining income. We begin taxing from the first dollar and proceed until all income is subject to tax based on the relevant brackets.
In Python, the function calculateTax iteratively computes the tax by subtracting from the income the amount taxed at each bracket. The variable previous_upper tracks the upper boundary of the last considered bracket to determine the current bracket size.
Python
C++
Java
C#
JavaScript
Time Complexity: O(n), where n is the number of tax brackets.
Space Complexity: O(1), since only a few auxiliary variables are used.
This approach involves iterating through each bracket while cumulatively summing the taxes, and exiting early if the full income has been taxed. It uses a cumulative difference strategy where each bracket contributes only the difference from the previous bracket.
In this Python solution, the algorithm checks if there's any income left to tax before continuing with processing a new bracket. It ensures minimal computation by bounding taxable income calculation by using min on upper and income.
Python
C++
Java
C#
JavaScript
Time Complexity: O(n), with n being the number of brackets.
Space Complexity: O(1), as no additional space proportionate to input size is required.
We traverse brackets, and for each tax bracket, we calculate the tax amount for that bracket, then accumulate it.
The time complexity is O(n), where n is the length of brackets. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Iterative Calculation of Taxes by Bracket | Time Complexity: O(n), where n is the number of tax brackets. |
| Cumulative Summation with Early Exit | Time Complexity: O(n), with n being the number of brackets. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Calculation of Taxes by Bracket | O(n) | O(1) | Standard solution when simulating progressive tax brackets |
| Cumulative Summation with Early Exit | O(n) | O(1) | Useful when income often falls in lower brackets and early termination saves iterations |
2303. Calculate Amount Paid in Taxes (Leetcode Easy) • Programming Live with Larry • 775 views views
Watch 4 more video solutions →Practice Calculate Amount Paid in Taxes with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor