Calculate Amount Paid in Taxes

You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the i^th tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound, meaning 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.
  • The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
  • And so on.

You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes.

Answers within 10^-5 of the actual answer will be accepted.

Example 1
Inputbrackets = [[3,50],[7,10],[12,25]], income = 10
Output2.65
Based on the income, 3 dollars are taxed at 50%, 4 dollars at 10%, and 3 dollars at 25%, for a total of $2.65 in taxes.
Example 2
Inputbrackets = [[1,0],[4,25],[5,50]], income = 2
Output0.25
Based on the income, 1 dollar is taxed at 0% and 1 dollar is taxed at 25%, for a total of $0.25 in taxes.

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate