Skip to content

Commit c7d02c3

Browse files
committed
feat: add solutions to lc problem: No.2303
No.2303.Calculate Amount Paid in Taxes
1 parent 90cb7b1 commit c7d02c3

File tree

7 files changed

+103
-159
lines changed

7 files changed

+103
-159
lines changed

solution/2300-2399/2303.Calculate Amount Paid in Taxes/README.md

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070

7171
**方法一:模拟**
7272

73+
遍历 `brackets`,对于每个税级,计算该税级的税额,然后累加即可。
74+
75+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `brackets` 的长度。
76+
7377
<!-- tabs:start -->
7478

7579
### **Python3**
@@ -79,18 +83,11 @@
7983
```python
8084
class Solution:
8185
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
82-
ans = idx = 0
83-
prev = 0
84-
while income:
85-
a, b = brackets[idx]
86-
d = a - prev
87-
ans += min(d, income) * b / 100
88-
if income <= d:
89-
break
90-
income -= d
91-
idx += 1
92-
prev = a
93-
return ans
86+
ans = prev = 0
87+
for upper, percent in brackets:
88+
ans += max(0, min(income, upper) - prev) * percent
89+
prev = upper
90+
return ans / 100
9491
```
9592

9693
### **Java**
@@ -100,20 +97,13 @@ class Solution:
10097
```java
10198
class Solution {
10299
public double calculateTax(int[][] brackets, int income) {
103-
double ans = 0;
104-
int idx = 0, prev = 0;
105-
while (income > 0) {
106-
int a = brackets[idx][0], b = brackets[idx][1];
107-
int d = a - prev;
108-
ans += Math.min(d, income) * b / 100.0;
109-
if (income <= d) {
110-
break;
111-
}
112-
income -= d;
113-
++idx;
114-
prev = a;
100+
int ans = 0, prev = 0;
101+
for (var e : brackets) {
102+
int upper = e[0], percent = e[1];
103+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
104+
prev = upper;
115105
}
116-
return ans;
106+
return ans / 100.0;
117107
}
118108
}
119109
```
@@ -124,18 +114,13 @@ class Solution {
124114
class Solution {
125115
public:
126116
double calculateTax(vector<vector<int>>& brackets, int income) {
127-
double ans = 0;
128-
int idx = 0, prev = 0;
129-
while (income) {
130-
int a = brackets[idx][0], b = brackets[idx][1];
131-
int d = a - prev;
132-
ans += min(d, income) * b / 100.0;
133-
if (income <= d) break;
134-
income -= d;
135-
++idx;
136-
prev = a;
117+
int ans = 0, prev = 0;
118+
for (auto& e : brackets) {
119+
int upper = e[0], percent = e[1];
120+
ans += max(0, min(income, upper) - prev) * percent;
121+
prev = upper;
137122
}
138-
return ans;
123+
return ans / 100.0;
139124
}
140125
};
141126
```
@@ -144,20 +129,20 @@ public:
144129
145130
```go
146131
func calculateTax(brackets [][]int, income int) float64 {
147-
var ans float64
148-
idx, prev := 0, 0
149-
for income > 0 {
150-
a, b := brackets[idx][0], brackets[idx][1]
151-
d := a - prev
152-
ans += float64(min(d, income)*b) / 100.0
153-
if income <= d {
154-
break
155-
}
156-
income -= d
157-
idx++
158-
prev = a
132+
var ans, prev int
133+
for _, e := range brackets {
134+
upper, percent := e[0], e[1]
135+
ans += max(0, min(income, upper)-prev) * percent
136+
prev = upper
159137
}
160-
return ans
138+
return float64(ans) / 100.0
139+
}
140+
141+
func max(a, b int) int {
142+
if a > b {
143+
return a
144+
}
145+
return b
161146
}
162147
163148
func min(a, b int) int {
@@ -193,12 +178,11 @@ impl Solution {
193178
function calculateTax(brackets: number[][], income: number): number {
194179
let ans = 0;
195180
let prev = 0;
196-
for (let [upper, percent] of brackets) {
197-
if (prev > income) break;
198-
ans += ((Math.min(upper, income) - prev) * percent) / 100;
181+
for (const [upper, percent] of brackets) {
182+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
199183
prev = upper;
200184
}
201-
return ans;
185+
return ans / 100;
202186
}
203187
```
204188

solution/2300-2399/2303.Calculate Amount Paid in Taxes/README_EN.md

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -71,39 +71,25 @@ You have no income to tax, so you have to pay a total of $0 in taxes.
7171
```python
7272
class Solution:
7373
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
74-
ans = idx = 0
75-
prev = 0
76-
while income:
77-
a, b = brackets[idx]
78-
d = a - prev
79-
ans += min(d, income) * b / 100
80-
if income <= d:
81-
break
82-
income -= d
83-
idx += 1
84-
prev = a
85-
return ans
74+
ans = prev = 0
75+
for upper, percent in brackets:
76+
ans += max(0, min(income, upper) - prev) * percent
77+
prev = upper
78+
return ans / 100
8679
```
8780

8881
### **Java**
8982

9083
```java
9184
class Solution {
9285
public double calculateTax(int[][] brackets, int income) {
93-
double ans = 0;
94-
int idx = 0, prev = 0;
95-
while (income > 0) {
96-
int a = brackets[idx][0], b = brackets[idx][1];
97-
int d = a - prev;
98-
ans += Math.min(d, income) * b / 100.0;
99-
if (income <= d) {
100-
break;
101-
}
102-
income -= d;
103-
++idx;
104-
prev = a;
86+
int ans = 0, prev = 0;
87+
for (var e : brackets) {
88+
int upper = e[0], percent = e[1];
89+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
90+
prev = upper;
10591
}
106-
return ans;
92+
return ans / 100.0;
10793
}
10894
}
10995
```
@@ -114,18 +100,13 @@ class Solution {
114100
class Solution {
115101
public:
116102
double calculateTax(vector<vector<int>>& brackets, int income) {
117-
double ans = 0;
118-
int idx = 0, prev = 0;
119-
while (income) {
120-
int a = brackets[idx][0], b = brackets[idx][1];
121-
int d = a - prev;
122-
ans += min(d, income) * b / 100.0;
123-
if (income <= d) break;
124-
income -= d;
125-
++idx;
126-
prev = a;
103+
int ans = 0, prev = 0;
104+
for (auto& e : brackets) {
105+
int upper = e[0], percent = e[1];
106+
ans += max(0, min(income, upper) - prev) * percent;
107+
prev = upper;
127108
}
128-
return ans;
109+
return ans / 100.0;
129110
}
130111
};
131112
```
@@ -134,20 +115,20 @@ public:
134115
135116
```go
136117
func calculateTax(brackets [][]int, income int) float64 {
137-
var ans float64
138-
idx, prev := 0, 0
139-
for income > 0 {
140-
a, b := brackets[idx][0], brackets[idx][1]
141-
d := a - prev
142-
ans += float64(min(d, income)*b) / 100.0
143-
if income <= d {
144-
break
145-
}
146-
income -= d
147-
idx++
148-
prev = a
118+
var ans, prev int
119+
for _, e := range brackets {
120+
upper, percent := e[0], e[1]
121+
ans += max(0, min(income, upper)-prev) * percent
122+
prev = upper
149123
}
150-
return ans
124+
return float64(ans) / 100.0
125+
}
126+
127+
func max(a, b int) int {
128+
if a > b {
129+
return a
130+
}
131+
return b
151132
}
152133
153134
func min(a, b int) int {
@@ -183,12 +164,11 @@ impl Solution {
183164
function calculateTax(brackets: number[][], income: number): number {
184165
let ans = 0;
185166
let prev = 0;
186-
for (let [upper, percent] of brackets) {
187-
if (prev > income) break;
188-
ans += ((Math.min(upper, income) - prev) * percent) / 100;
167+
for (const [upper, percent] of brackets) {
168+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
189169
prev = upper;
190170
}
191-
return ans;
171+
return ans / 100;
192172
}
193173
```
194174

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
class Solution {
22
public:
33
double calculateTax(vector<vector<int>>& brackets, int income) {
4-
double ans = 0;
5-
int idx = 0, prev = 0;
6-
while (income) {
7-
int a = brackets[idx][0], b = brackets[idx][1];
8-
int d = a - prev;
9-
ans += min(d, income) * b / 100.0;
10-
if (income <= d) break;
11-
income -= d;
12-
++idx;
13-
prev = a;
4+
int ans = 0, prev = 0;
5+
for (auto& e : brackets) {
6+
int upper = e[0], percent = e[1];
7+
ans += max(0, min(income, upper) - prev) * percent;
8+
prev = upper;
149
}
15-
return ans;
10+
return ans / 100.0;
1611
}
1712
};

solution/2300-2399/2303.Calculate Amount Paid in Taxes/Solution.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
func calculateTax(brackets [][]int, income int) float64 {
2-
var ans float64
3-
idx, prev := 0, 0
4-
for income > 0 {
5-
a, b := brackets[idx][0], brackets[idx][1]
6-
d := a - prev
7-
ans += float64(min(d, income)*b) / 100.0
8-
if income <= d {
9-
break
10-
}
11-
income -= d
12-
idx++
13-
prev = a
2+
var ans, prev int
3+
for _, e := range brackets {
4+
upper, percent := e[0], e[1]
5+
ans += max(0, min(income, upper)-prev) * percent
6+
prev = upper
147
}
15-
return ans
8+
return float64(ans) / 100.0
9+
}
10+
11+
func max(a, b int) int {
12+
if a > b {
13+
return a
14+
}
15+
return b
1616
}
1717

1818
func min(a, b int) int {
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
class Solution {
22
public double calculateTax(int[][] brackets, int income) {
3-
double ans = 0;
4-
int idx = 0, prev = 0;
5-
while (income > 0) {
6-
int a = brackets[idx][0], b = brackets[idx][1];
7-
int d = a - prev;
8-
ans += Math.min(d, income) * b / 100.0;
9-
if (income <= d) {
10-
break;
11-
}
12-
income -= d;
13-
++idx;
14-
prev = a;
3+
int ans = 0, prev = 0;
4+
for (var e : brackets) {
5+
int upper = e[0], percent = e[1];
6+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
7+
prev = upper;
158
}
16-
return ans;
9+
return ans / 100.0;
1710
}
1811
}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
class Solution:
22
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
3-
ans = idx = 0
4-
prev = 0
5-
while income:
6-
a, b = brackets[idx]
7-
d = a - prev
8-
ans += min(d, income) * b / 100
9-
if income <= d:
10-
break
11-
income -= d
12-
idx += 1
13-
prev = a
14-
return ans
3+
ans = prev = 0
4+
for upper, percent in brackets:
5+
ans += max(0, min(income, upper) - prev) * percent
6+
prev = upper
7+
return ans / 100
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
function calculateTax(brackets: number[][], income: number): number {
22
let ans = 0;
33
let prev = 0;
4-
for (let [upper, percent] of brackets) {
5-
if (prev > income) break;
6-
ans += ((Math.min(upper, income) - prev) * percent) / 100;
4+
for (const [upper, percent] of brackets) {
5+
ans += Math.max(0, Math.min(income, upper) - prev) * percent;
76
prev = upper;
87
}
9-
return ans;
8+
return ans / 100;
109
}

0 commit comments

Comments
 (0)