#2303
Easy Algorithms Calculate amount paid in taxes
Array Simulation
69.0% acceptance
Feb 25, 2026
285
297
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.
And so on.
You are given an integer income. Return the amount of money you have to pay in taxes.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn calculate_tax(brackets: Vec<Vec<i32>>, income: i32) -> f64 {
let mut tax = 0.0f64;
let mut prev = 0;
for b in &brackets {
let upper = b[0];
let percent = b[1];
let taxable = (income.min(upper) - prev).max(0);
tax += taxable as f64 * percent as f64 / 100.0;
prev = upper;
if income <= upper { break; }
}
tax
}
}