Show problems as
Classic problem #2303EasyArraySimulation

Calculate Amount Paid in Taxes

Real-world version: Graduated Service Levy
Tracked in this browser only
Read Calculate Amount Paid in Taxes on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A credit union bills its members a graduated service levy against their declared annual income.

You are handed the schedule as brackets. Entry i of it holds two numbers: a ceiling upper_i followed by a rate percent_i, and together they describe the i-th band of the schedule. The bands are listed with upper_i increasing. Band 0 covers income from 0 up to and including upper_0. For i > 0, band i covers the income above upper_(i-1) up to and including upper_i. Whatever portion of the income lands inside band i is billed at percent_i percent of that portion.

Given a member's income, return the levy owed on it.

The levy always works out to a whole number of hundredths of a currency unit. Return it as a decimal number; an answer is accepted when it is within 10^-5 of the correct levy.

Examples

Example 1

Input
brackets = [[3, 50], [7, 10], [12, 25]], income = 10
Output
2.65

The first 3 of income is billed at 50 percent, giving 1.5. The next 4 is billed at 10 percent, giving 0.4. The remaining 3 falls in the third band at 25 percent, giving 0.75. Altogether that is 2.65.

Example 2

Input
brackets = [[4, 0], [9, 100]], income = 9
Output
5.0

The first 4 of income attracts nothing, and the remaining 5 is billed in full, so the levy is 5.0.

Example 3

Input
brackets = [[500, 20], [1000, 40]], income = 100
Output
20.0

All 100 of income sits inside the first band, so it is billed at 20 percent for a levy of 20.0. The second band is never reached.

Constraints

  • 1 <= brackets.length <= 100
  • 1 <= upper_i <= 1000
  • 0 <= percent_i <= 100
  • 0 <= income <= 1000
  • upper_i is sorted in ascending order.
  • All the values of upper_i are unique.
  • The upper bound of the last band is greater than or equal to income.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def calculate_tax(brackets: list[list[int]], income: int) -> float:
Java
public double calculateTax(int[][] brackets, int income)