Class Solution
java.lang.Object
g2301_2400.s2303_calculate_amount_paid_in_taxes.Solution
2303 - Calculate Amount Paid in Taxes.<p>Easy</p>
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>brackets</code> where <code>brackets[i] = [upper<sub>i</sub>, percent<sub>i</sub>]</code> means that the <code>i<sup>th</sup></code> tax bracket has an upper bound of <code>upper<sub>i</sub></code> and is taxed at a rate of <code>percent<sub>i</sub></code>. The brackets are <strong>sorted</strong> by upper bound (i.e. <code>upper<sub>i-1</sub> < upper<sub>i</sub></code> for <code>0 < i < brackets.length</code>).</p>
<p>Tax is calculated as follows:</p>
<ul>
<li>The first <code>upper<sub>0</sub></code> dollars earned are taxed at a rate of <code>percent<sub>0</sub></code>.</li>
<li>The next <code>upper<sub>1</sub> - upper<sub>0</sub></code> dollars earned are taxed at a rate of <code>percent<sub>1</sub></code>.</li>
<li>The next <code>upper<sub>2</sub> - upper<sub>1</sub></code> dollars earned are taxed at a rate of <code>percent<sub>2</sub></code>.</li>
<li>And so on.</li>
</ul>
<p>You are given an integer <code>income</code> representing the amount of money you earned. Return <em>the amount of money that you have to pay in taxes.</em> Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> brackets = [[3,50],[7,10],[12,25]], income = 10</p>
<p><strong>Output:</strong> 2.65000</p>
<p><strong>Explanation:</strong></p>
<p>The first 3 dollars you earn are taxed at 50%. You have to pay $3 * 50% = $1.50 dollars in taxes.</p>
<p>The next 7 - 3 = 4 dollars you earn are taxed at 10%. You have to pay $4 * 10% = $0.40 dollars in taxes.</p>
<p>The final 10 - 7 = 3 dollars you earn are taxed at 25%. You have to pay $3 * 25% = $0.75 dollars in taxes. You have to pay a total of $1.50 + $0.40 + $0.75 = $2.65 dollars in taxes.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> brackets = [[1,0],[4,25],[5,50]], income = 2</p>
<p><strong>Output:</strong> 0.25000</p>
<p><strong>Explanation:</strong></p>
<p>The first dollar you earn is taxed at 0%. You have to pay $1 * 0% = $0 dollars in taxes.</p>
<p>The second dollar you earn is taxed at 25%. You have to pay $1 * 25% = $0.25 dollars in taxes.</p>
<p>You have to pay a total of $0 + $0.25 = $0.25 dollars in taxes.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> brackets = [[2,50]], income = 0</p>
<p><strong>Output:</strong> 0.00000</p>
<p><strong>Explanation:</strong> You have no income to tax, so you have to pay a total of $0 dollars in taxes.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= brackets.length <= 100</code></li>
<li><code>1 <= upper<sub>i</sub> <= 1000</code></li>
<li><code>0 <= percent<sub>i</sub> <= 100</code></li>
<li><code>0 <= income <= 1000</code></li>
<li><code>upper<sub>i</sub></code> is sorted in ascending order.</li>
<li>All the values of <code>upper<sub>i</sub></code> are <strong>unique</strong>.</li>
<li>The upper bound of the last tax bracket is greater than or equal to <code>income</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
calculateTax
public double calculateTax(int[][] brackets, int income)
-