#2303

Calculate Amount Paid in Taxes

pupil · 355 · lc easy +23 · verified · 69.1% accepted · 285 likes · top 77%

Description

You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] describes a tax bracket with upper bound upperi taxed at percenti%. Brackets are sorted by upper bound.

Tax is calculated as follows:

- The first upper0 dollars earned are taxed at percent0%.

- The next upper1 - upper0 dollars are taxed at percent1%.

- The next upper2 - upper1 dollars are taxed at percent2%.

- And so on.

Given an integer income, return the total tax owed. Answers within 10-5 of the exact value are 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.

Code

1
2
3