当前位置:网站首页>LeetCode 5259. Calculate the total tax payable

LeetCode 5259. Calculate the total tax payable

2022-06-13 09:18:00 Michael Amin

List of articles

1. subject

I'll give you a subscript from 0 Start with a two-dimensional integer array brackets , among brackets[i] = [upperi, percenti] , It means the first one i The upper limit of individual income tax is upperi , The tax rate charged is percenti . Tax level by upper limit Sort from low to high ( In the meet 0 < i < brackets.length Under the premise of ,upperi-1 < upperi).

The tax is calculated as follows :

  • No more than upper0 Income is taxed percent0 Pay
  • next upper1 - upper0 The part of the tax rate is percent1 Pay
  • then upper2 - upper1 The part of the tax rate is percent2 Pay And so on

Give you an integer income Represents your total income . Return the total amount of tax you have to pay . The error with the standard answer does not exceed 10-5 The result of will be regarded as the correct answer .

 Example  1:
 Input :brackets = [[3,50],[7,10],[12,25]], income = 10
 Output :2.65000
 explain :
 front  $3  The tax rate is  50% . Taxes are payable  $3 * 50% = $1.50 .
 Next  $7 - $3 = $4  The tax rate is  10% . Taxes are payable  $4 * 10% = $0.40 .
 Last  $10 - $7 = $3  The tax rate is  25% . Taxes are payable  $3 * 25% = $0.75 .
 Total taxes to be paid  $1.50 + $0.40 + $0.75 = $2.65 .

 Example  2:
 Input :brackets = [[1,0],[4,25],[5,50]], income = 2
 Output :0.25000
 explain :
 front  $1  The tax rate is  0% . Taxes are payable  $1 * 0% = $0 .
 be left over  $1  The tax rate is  25% . Taxes are payable  $1 * 25% = $0.25 .
 Total taxes to be paid  $0 + $0.25 = $0.25 .

 Example  3:
 Input :brackets = [[2,50]], income = 0
 Output :0.00000
 explain :
 No income , No tax is required , Total taxes to be paid  $0 .
 
 Tips :
1 <= brackets.length <= 100
1 <= upperi <= 1000
0 <= percenti <= 100
0 <= income <= 1000
upperi  In ascending order 
upperi  All the values in   Different from each other 
 The upper limit of the last tax level is greater than or equal to  income

source : Power button (LeetCode) link :https://leetcode.cn/problems/calculate-amount-paid-in-taxes Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

2. Problem solving

  • Imitate according to the meaning of the topic
class Solution:
    def calculateTax(self, brackets: List[List[int]], income: int) -> float:
        ans = 0
        prev = 0
        for i in range(len(brackets)):
            if income > 0:
                val = brackets[i][0] - prev #  Interval tax amount 
                money = min(val, income) #  The amount to be taxed 
                ans += money*brackets[i][1]/100 #  pay taxes 
                income -= money #  Update the remaining amount to be taxed 
                prev = brackets[i][0] #  Tax breakpoints 
            else:
                break
        return ans

44 ms 15.1 MB Python3


my CSDN Blog address https://michael.blog.csdn.net/

原网站

版权声明
本文为[Michael Amin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130905214271.html