You are given an array of tax brackets
and an integer income
. Each tax bracket is represented as a list of three integers: [upper, percent]
, where upper
is the upper bound of the bracket (inclusive), and percent
is the tax rate for that bracket (as a percentage). The brackets are sorted by upper bound in ascending order.
Your task is to calculate the total amount of tax paid for the given income
according to the tax brackets. Tax is calculated progressively: for each bracket, only the portion of income within that bracket's range is taxed at the bracket's rate.
Key constraints:
income
.double
or floating-point value.At first glance, the problem appears to require simulating a real-world progressive tax system. For each tax bracket, we need to determine how much of the income falls within that bracket and then apply the bracket's tax rate to that portion.
A brute-force approach might try to simulate every dollar or unit of income, but that's inefficient and unnecessary. Instead, we can process each bracket in order, calculate the taxable amount for that bracket, and accumulate the total tax. We need to be careful to only tax the portion of income that falls within each bracket, not the entire income at every step.
The main challenge is to correctly handle the boundaries between brackets and stop once we've processed all of the income.
We will process the tax brackets one by one, keeping track of the previous bracket's upper bound (starting from 0). For each bracket:
min(income, upper) - previous_upper
.
portion * percent / 100.0
.
previous_upper
to upper
for the next bracket.
income
is less than or equal to upper
, we can break out of the loop since all income has been taxed.
This approach is efficient because it processes each bracket at most once, and only computes the tax for the relevant portion of income.
Suppose brackets = [[3,50],[7,10],[12,25]]
and income = 10
.
upper = 3
, percent = 50
min(10, 3) - 0 = 3
3 * 50 / 100 = 1.5
1.5
upper = 7
, percent = 10
min(10, 7) - 3 = 4
4 * 10 / 100 = 0.4
1.9
upper = 12
, percent = 25
min(10, 12) - 7 = 3
3 * 25 / 100 = 0.75
2.65
income = 10
is less than upper = 12
, we're done.
The answer is 2.65
.
Brute-force approach:
O(income)
. This is inefficient for large incomes.
O(N)
, where N
is the number of brackets.
O(1)
as we only use a few variables for bookkeeping.
The optimized approach is both efficient and simple.
This problem is a simulation of progressive taxation. The key insight is to process each bracket in order, only taxing the portion of income that falls within each bracket. By keeping track of the previous upper bound and stopping early when all income is processed, we ensure both correctness and efficiency. The solution is elegant because it directly models the real-world process and avoids unnecessary computation.
class Solution:
def calculateTax(self, brackets, income):
tax = 0.0
prev = 0
for upper, percent in brackets:
taxable = min(income, upper) - prev
if taxable <= 0:
break
tax += taxable * percent / 100.0
prev = upper
if income <= upper:
break
return tax
class Solution {
public:
double calculateTax(vector<vector<int>>& brackets, int income) {
double tax = 0.0;
int prev = 0;
for (auto& bracket : brackets) {
int upper = bracket[0], percent = bracket[1];
int taxable = min(income, upper) - prev;
if (taxable <= 0) break;
tax += taxable * percent / 100.0;
prev = upper;
if (income <= upper) break;
}
return tax;
}
};
class Solution {
public double calculateTax(int[][] brackets, int income) {
double tax = 0.0;
int prev = 0;
for (int[] bracket : brackets) {
int upper = bracket[0], percent = bracket[1];
int taxable = Math.min(income, upper) - prev;
if (taxable <= 0) break;
tax += taxable * percent / 100.0;
prev = upper;
if (income <= upper) break;
}
return tax;
}
}
var calculateTax = function(brackets, income) {
let tax = 0.0;
let prev = 0;
for (const [upper, percent] of brackets) {
let taxable = Math.min(income, upper) - prev;
if (taxable <= 0) break;
tax += taxable * percent / 100.0;
prev = upper;
if (income <= upper) break;
}
return tax;
};