|
1 | 1 | // https://leetcode.com/problems/calculate-amount-paid-in-taxes
|
2 |
| -// |
| 2 | +// |
3 | 3 | // You are given a **0-indexed** 2D integer array `brackets` where `brackets[i] = [upper<sub>i</sub>, percent<sub>i</sub>]` means that the `i<sup>th</sup>` tax bracket has an upper bound of `upper<sub>i</sub>` and is taxed at a rate of `percent<sub>i</sub>`. The brackets are **sorted** by upper bound (i.e. `upper<sub>i-1</sub> < upper<sub>i</sub>` for `0 < i < brackets.length`).
|
4 |
| -// |
| 4 | +// |
5 | 5 | // Tax is calculated as follows:
|
6 |
| -// |
| 6 | +// |
7 | 7 | // * The first `upper<sub>0</sub>` dollars earned are taxed at a rate of `percent<sub>0</sub>`.
|
8 | 8 | // * The next `upper<sub>1</sub> - upper<sub>0</sub>` dollars earned are taxed at a rate of `percent<sub>1</sub>`.
|
9 | 9 | // * The next `upper<sub>2</sub> - upper<sub>1</sub>` dollars earned are taxed at a rate of `percent<sub>2</sub>`.
|
10 | 10 | // * And so on.
|
11 |
| -// |
| 11 | +// |
12 | 12 | // 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<sup>-5</sup>` of the actual answer will be accepted.
|
13 |
| -// |
| 13 | +// |
14 | 14 | // **Example 1:**
|
15 |
| -// |
| 15 | +// |
16 | 16 | // ```
|
17 | 17 | // **Input:** brackets = [[3,50],[7,10],[12,25]], income = 10
|
18 | 18 | // **Output:** 2.65000
|
|
21 | 21 | // The tax rate for the three tax brackets is 50%, 10%, and 25%, respectively.
|
22 | 22 | // In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.
|
23 | 23 | // ```
|
24 |
| -// |
| 24 | +// |
25 | 25 | // **Example 2:**
|
26 |
| -// |
| 26 | +// |
27 | 27 | // ```
|
28 | 28 | // **Input:** brackets = [[1,0],[4,25],[5,50]], income = 2
|
29 | 29 | // **Output:** 0.25000
|
|
32 | 32 | // The tax rate for the two tax brackets is 0% and 25%, respectively.
|
33 | 33 | // In total, you pay $1 * 0% + $1 * 25% = $0.25 in taxes.
|
34 | 34 | // ```
|
35 |
| -// |
| 35 | +// |
36 | 36 | // **Example 3:**
|
37 |
| -// |
| 37 | +// |
38 | 38 | // ```
|
39 | 39 | // **Input:** brackets = [[2,50]], income = 0
|
40 | 40 | // **Output:** 0.00000
|
41 | 41 | // **Explanation:**
|
42 | 42 | // You have no income to tax, so you have to pay a total of $0 in taxes.
|
43 | 43 | // ```
|
44 |
| -// |
| 44 | +// |
45 | 45 | // **Constraints:**
|
46 |
| -// |
| 46 | +// |
47 | 47 | // * `1 <= brackets.length <= 100`
|
48 | 48 | // * `1 <= upper<sub>i</sub> <= 1000`
|
49 | 49 | // * `0 <= percent<sub>i</sub> <= 100`
|
|
53 | 53 | // * The upper bound of the last tax bracket is greater than or equal to `income`.
|
54 | 54 |
|
55 | 55 | pub fn calculate_tax(brackets: Vec<Vec<i32>>, income: i32) -> f64 {
|
56 |
| - |
| 56 | + let mut tax = 0.0; |
| 57 | + let mut prev_upper = 0; |
| 58 | + for bracket in brackets { |
| 59 | + let (upper, percent) = (bracket[0], bracket[1]); |
| 60 | + if income > upper { |
| 61 | + tax += (upper - prev_upper) as f64 * percent as f64 / 100.0; |
| 62 | + prev_upper = upper; |
| 63 | + } else { |
| 64 | + tax += (income - prev_upper) as f64 * percent as f64 / 100.0; |
| 65 | + break; |
| 66 | + } |
57 | 67 | }
|
| 68 | + return tax; |
| 69 | +} |
58 | 70 |
|
59 | 71 | #[test]
|
60 | 72 | pub fn t1() {
|
| 73 | + assert_eq!(calculate_tax(vec![vec![3, 50], vec![7, 10], vec![12, 25]], 10), 2.65); |
61 | 74 | }
|
0 commit comments