Skip to content

Commit f3069e3

Browse files
committed
feat: add more
1 parent a15ec89 commit f3069e3

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// https://leetcode.com/problems/calculate-amount-paid-in-taxes
2-
//
2+
//
33
// 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+
//
55
// Tax is calculated as follows:
6-
//
6+
//
77
// * The first `upper<sub>0</sub>` dollars earned are taxed at a rate of `percent<sub>0</sub>`.
88
// * The next `upper<sub>1</sub> - upper<sub>0</sub>` dollars earned are taxed at a rate of `percent<sub>1</sub>`.
99
// * The next `upper<sub>2</sub> - upper<sub>1</sub>` dollars earned are taxed at a rate of `percent<sub>2</sub>`.
1010
// * And so on.
11-
//
11+
//
1212
// 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+
//
1414
// **Example 1:**
15-
//
15+
//
1616
// ```
1717
// **Input:** brackets = [[3,50],[7,10],[12,25]], income = 10
1818
// **Output:** 2.65000
@@ -21,9 +21,9 @@
2121
// The tax rate for the three tax brackets is 50%, 10%, and 25%, respectively.
2222
// In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.
2323
// ```
24-
//
24+
//
2525
// **Example 2:**
26-
//
26+
//
2727
// ```
2828
// **Input:** brackets = [[1,0],[4,25],[5,50]], income = 2
2929
// **Output:** 0.25000
@@ -32,18 +32,18 @@
3232
// The tax rate for the two tax brackets is 0% and 25%, respectively.
3333
// In total, you pay $1 * 0% + $1 * 25% = $0.25 in taxes.
3434
// ```
35-
//
35+
//
3636
// **Example 3:**
37-
//
37+
//
3838
// ```
3939
// **Input:** brackets = [[2,50]], income = 0
4040
// **Output:** 0.00000
4141
// **Explanation:**
4242
// You have no income to tax, so you have to pay a total of $0 in taxes.
4343
// ```
44-
//
44+
//
4545
// **Constraints:**
46-
//
46+
//
4747
// * `1 <= brackets.length <= 100`
4848
// * `1 <= upper<sub>i</sub> <= 1000`
4949
// * `0 <= percent<sub>i</sub> <= 100`
@@ -53,9 +53,22 @@
5353
// * The upper bound of the last tax bracket is greater than or equal to `income`.
5454

5555
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+
}
5767
}
68+
return tax;
69+
}
5870

5971
#[test]
6072
pub fn t1() {
73+
assert_eq!(calculate_tax(vec![vec![3, 50], vec![7, 10], vec![12, 25]], 10), 2.65);
6174
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ mod _2273_find_resultant_array_after_removing_anagrams;
165165
mod _2293_min_max_game;
166166
mod _2319_check_if_matrix_is_x_matrix;
167167
mod _2335_minimum_amount_of_time_to_fill_cups;
168+
mod _2303_calculate_amount_paid_in_taxes;

0 commit comments

Comments
 (0)