You are given a 0-indexed 2D integer array brackets
where brackets[i] = [upperi, percenti]
means that the ith
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
).
Tax is calculated as follows:
- The first
upper0
dollars earned are taxed at a rate ofpercent0
. - The next
upper1 - upper0
dollars earned are taxed at a rate ofpercent1
. - The next
upper2 - upper1
dollars earned are taxed at a rate ofpercent2
. - 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:
Input: brackets = [[3,50],[7,10],[12,25]], income = 10 Output: 2.65000 Explanation: Based on your income, you have 3 dollars in the 1st tax bracket, 4 dollars in the 2nd tax bracket, and 3 dollars in the 3rd tax bracket. The tax rate for the three tax brackets is 50%, 10%, and 25%, respectively. In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.
Example 2:
Input: brackets = [[1,0],[4,25],[5,50]], income = 2 Output: 0.25000 Explanation: Based on your income, you have 1 dollar in the 1st tax bracket and 1 dollar in the 2nd tax bracket. The tax rate for the two tax brackets is 0% and 25%, respectively. In total, you pay $1 * 0% + $1 * 25% = $0.25 in taxes.
Example 3:
Input: brackets = [[2,50]], income = 0 Output: 0.00000 Explanation: You have no income to tax, so you have to pay a total of $0 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
.
计算应缴税款总额。
给你一个下标从 0 开始的二维整数数组 brackets ,其中 brackets[i] = [upperi, percenti] ,表示第 i 个税级的上限是 upperi ,征收的税率为 percenti 。税级按上限 从低到高排序(在满足 0 < i < brackets.length 的前提下,upperi-1 < upperi)。
税款计算方式如下:
不超过 upper0 的收入按税率 percent0 缴纳
接着 upper1 - upper0 的部分按税率 percent1 缴纳
然后 upper2 - upper1 的部分按税率 percent2 缴纳
以此类推
给你一个整数 income 表示你的总收入。返回你需要缴纳的税款总额。与标准答案误差不超 10-5 的结果将被视作正确答案。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/calculate-amount-paid-in-taxes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是模拟。你的收入是 income,但是税率是按级别来收的。所以我们需要遍历整个 brackets 数组,把当前的税级 upper = b[0] 和 税率 rate = b[1] 找到。我们需要一个变量 prev 记录上一个税级的上限是多少。然后判断,
- 如果 income 大于等于当前的税级 upper,说明当前税级是要满额收税的
- 如果 income 小于当前的税级,那么这一段的税是用 income大于上一个税级的部分去计算, (income - prev) * rate
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public double calculateTax(int[][] brackets, int income) { 3 double res = 0; 4 int prev = 0; 5 for (int[] b : brackets) { 6 int upper = b[0]; 7 int rate = b[1]; 8 if (income >= upper) { 9 res += (upper - prev) * rate / 100.0; 10 prev = upper; 11 } else { 12 res += (income - prev) * rate / 100.0; 13 return res; 14 } 15 } 16 return res; 17 } 18 }
标签:upper,upperi,Calculate,brackets,2303,tax,Taxes,rate,income From: https://www.cnblogs.com/cnoodle/p/17064889.html