2303. Calculate Amount Paid in Taxes Easy

@problem@discussion
#Array#Simulation



1/**
2 * [2303] Calculate Amount Paid in Taxes
3 *
4 * 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 (i.e. upperi-1 < upperi for 0 < i < brackets.length).
5 * Tax is calculated as follows:
6 *
7 * The first upper0 dollars earned are taxed at a rate of percent0.
8 * The next upper1 - upper0 dollars earned are taxed at a rate of percent1.
9 * The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
10 * And so on.
11 *
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^-5 of the actual answer will be accepted.
13 *  
14 * Example 1:
15 *
16 * Input: brackets = [[3,50],[7,10],[12,25]], income = 10
17 * Output: 2.65000
18 * Explanation:
19 * The first 3 dollars you earn are taxed at 50%. You have to pay $3 * 50% = $1.50 dollars in taxes.
20 * The next 7 - 3 = 4 dollars you earn are taxed at 10%. You have to pay $4 * 10% = $0.40 dollars in taxes.
21 * The final 10 - 7 = 3 dollars you earn are taxed at 25%. You have to pay $3 * 25% = $0.75 dollars in taxes.
22 * You have to pay a total of $1.50 + $0.40 + $0.75 = $2.65 dollars in taxes.
23 *
24 * Example 2:
25 *
26 * Input: brackets = [[1,0],[4,25],[5,50]], income = 2
27 * Output: 0.25000
28 * Explanation:
29 * The first dollar you earn is taxed at 0%. You have to pay $1 * 0% = $0 dollars in taxes.
30 * The second dollar you earn is taxed at 25%. You have to pay $1 * 25% = $0.25 dollars in taxes.
31 * You have to pay a total of $0 + $0.25 = $0.25 dollars in taxes.
32 *
33 * Example 3:
34 *
35 * Input: brackets = [[2,50]], income = 0
36 * Output: 0.00000
37 * Explanation:
38 * You have no income to tax, so you have to pay a total of $0 dollars in taxes.
39 *
40 *  
41 * Constraints:
42 *
43 * 1 <= brackets.length <= 100
44 * 1 <= upperi <= 1000
45 * 0 <= percenti <= 100
46 * 0 <= income <= 1000
47 * upperi is sorted in ascending order.
48 * All the values of upperi are unique.
49 * The upper bound of the last tax bracket is greater than or equal to income.
50 *
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/calculate-amount-paid-in-taxes/
55// discuss: https://leetcode.com/problems/calculate-amount-paid-in-taxes/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn calculate_tax(brackets: Vec<Vec<i32>>, income: i32) -> f64 {
61        let mut result = 0f64;
62
63        for (i, v) in brackets.iter().enumerate() {
64            let pre = if i == 0 { 0 } else { brackets[i - 1][0] };
65            let end = if income > v[0] { v[0] } else { income };
66            result += (end - pre) as f64 / 100f64 * v[1] as f64;
67
68            if income <= v[0] {
69                break;
70            }
71        }
72        let last = brackets.last().unwrap();
73        if income > last[0] {
74            result += (income - last[0]) as f64 / 100f64 * last[1] as f64;
75        }
76        result
77    }
78}
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_2303() {
88        assert_eq!(
89            Solution::calculate_tax(vec![vec![3, 50], vec![7, 10], vec![12, 25]], 10),
90            2.65
91        );
92    }
93
94    #[test]
95    fn test_2303_1() {
96        assert_eq!(
97            Solution::calculate_tax(vec![vec![1, 0], vec![4, 25], vec![5, 50]], 2),
98            0.25
99        );
100    }
101}
102


Back
© 2025 bowen.ge All Rights Reserved.