avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2303. Calculate the total taxable amount

  2024-01-01        
字数统计: 270字   |   阅读时长: 1min

2023-01-23 (2).png
2303. Calculate the total taxable amount

Thought:

Sit a long time,It feels rare“Simple”。Not to do,The solution of the question。There are many judgments,So I wrote a lot of judgments,The more you write, the more you feel wrong。

Let’s take a look at the official solution

Code:

错误Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import List

class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
if income == 0:
return 0
tax: float = 0
index: int = 1
if income < brackets[0][0]:
return income * brackets[0][1] / 100
else:
tax += brackets[0][0] * brackets[0][1] / 100
while index < len(brackets):
# Judging whether it is the last one
if index == len(brackets) - 1:
# ComparebracketsThe last one is still big
if income - brackets[index][0] < 0:
tax += (income - brackets[index - 1][0]) * brackets[index][1] / 100
return tax
if income >= brackets[index][0]:
tax += (brackets[index][0]- brackets[index-1][0]) * brackets[index][1] / 100
if income < brackets[index-1][0]:
break
index += 1
return tax

print(Solution.calculateTax(Solution(), brackets = [[1,0],[4,25],[5,50]], income = 2))

Let’s take a look at the official solution

1
2
3
4
5
6
7
8
9
10
class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
totalTax = lower = 0
for upper, percent in brackets:
tax = (min(income, upper) - lower) * percent
totalTax += tax
if income <= upper:
break
lower = upper
return totalTax / 100

@ylb

1
2
3
4
5
6
7
class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
ans = prev = 0
for upper, percent in brackets:
ans += max(0, min(income, upper) - prev) * percent
prev = upper
return ans / 100
  • Python
  • math
  • unsolved

扫一扫,分享到微信

微信分享二维码
One question daily 2293. Great mini game
One question daily 2299. Code inspection device II
目录
  1. 1. Thought:
  2. 2. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号