当前位置:网站首页>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 taxedpercent0
Pay - next
upper1 - upper0
The part of the tax rate ispercent1
Pay - then
upper2 - upper1
The part of the tax rate ispercent2
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
边栏推荐
猜你喜欢
Drill down to protobuf - Introduction
CAS NO lock
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
线上调试工具Arthas基础
Alibaba senior experts analyze the standard design of protocol
Simulink如何添加模块到Library Browser
C language: deep understanding of pointers and arrays
SQL ROW_ The number() function uses
IP address introduction
Message Oriented Middleware
随机推荐
阿里高级专家剖析 | Protocol的标准设计
Neo4j - CQL使用
LeetCode 剑指 Offer 30. 包含min函数的栈
C/S模型与P2P模型
C language: summary of question brushing (1)
20211006 linear transformation
Qvector shallow copy performance test
Agile development practice summary-4
Dpdk timer learning notes
Library management system based on wechat applet Rar (thesis + source code)
20211006 线性变换
20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
Simulink variant model and variant subsystem usage
C language: five custom types
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
JUC 原子累加器
BGP Federation +community
批量读取文件夹下的全部语音文件
Neo4j環境搭建
final 原理