Calculate Your Tax Bill
Imagine you're a tax accountant working for a financial consulting firm. Your job is to calculate exactly how much tax someone owes based on a progressive tax system.
You are given a brackets array where each element [upper_bound, tax_rate] represents a tax bracket. The brackets are sorted by upper bound, and taxes are calculated in a tiered system:
- 💰 The first
upper₀dollars are taxed atpercent₀% - 💰 The next
(upper₁ - upper₀)dollars are taxed atpercent₁% - 💰 The next
(upper₂ - upper₁)dollars are taxed atpercent₂% - 💰 And so on...
Goal: Given someone's total income, calculate the total amount of tax they need to pay.
Example: If brackets are [[3,50], [7,10], [12,25]] and income is 10:
• First $3 taxed at 50% = $1.50
• Next $(7-3) = $4 taxed at 10% = $0.40
• Next $(10-7) = $3 taxed at 25% = $0.75
• Total tax = $1.50 + $0.40 + $0.75 = $2.65
Input & Output
Visualization
Time & Space Complexity
We iterate through each tax bracket once, where n is the number of brackets
We only use a constant amount of extra space for variables
Constraints
- 1 ≤ brackets.length ≤ 100
- 1 ≤ upperi ≤ 1000
- 0 ≤ percenti ≤ 100
- 0 ≤ income ≤ 1000
- brackets is sorted in ascending order by upperi
- All the values of upperi are unique