Skip to content

Commit 3c80be4

Browse files
committed
Add solution and test-cases for problem 2240
1 parent f03226b commit 3c80be4

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2240.Number of Ways to Buy Pens and Pencils][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
You are given an integer `total` indicating the amount of money you have. You are also given two integers `cost1` and `cost2` indicating the price of a pen and pencil respectively. You can spend **part or all** of your money to buy multiple quantities (or none) of each kind of writing utensil.
6+
7+
Return the **number of distinct ways** you can buy some number of pens and pencils.
8+
89
**Example 1:**
910

1011
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
12+
Input: total = 20, cost1 = 10, cost2 = 5
13+
Output: 9
14+
Explanation: The price of a pen is 10 and the price of a pencil is 5.
15+
- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.
16+
- If you buy 1 pen, you can buy 0, 1, or 2 pencils.
17+
- If you buy 2 pens, you cannot buy any pencils.
18+
The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Number of Ways to Buy Pens and Pencils
23-
```go
2423
```
25-
24+
Input: total = 5, cost1 = 10, cost2 = 10
25+
Output: 1
26+
Explanation: The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.
27+
```
2628

2729
## 结语
2830

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(total int, cost1 int, cost2 int) int64 {
4+
var ret int64
5+
penCount := 0
6+
for ; penCount*cost1 <= total; penCount++ {
7+
left := (total-penCount*cost1)/cost2 + 1 // 0
8+
ret += int64(left)
9+
}
10+
return ret
511
}

leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
total, cost1, cost2 int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 20, 10, 5, 9},
17+
{"TestCase2", 5, 10, 10, 1},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.total, c.cost1, c.cost2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
26+
c.expect, got, c.total, c.cost1, c.cost2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)