From 3c80be4c5bc5fb6cd3da22d2fa04280cc31b9488 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 26 Oct 2025 20:19:13 +0800 Subject: [PATCH] Add solution and test-cases for problem 2240 --- .../README.md | 30 ++++++++++--------- .../Solution.go | 10 +++++-- .../Solution_test.go | 21 +++++++------ 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/README.md b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/README.md index 90aa539aa..e715b72ac 100755 --- a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/README.md +++ b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/README.md @@ -1,28 +1,30 @@ # [2240.Number of Ways to Buy Pens and Pencils][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. + +Return the **number of distinct ways** you can buy some number of pens and pencils. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: total = 20, cost1 = 10, cost2 = 5 +Output: 9 +Explanation: The price of a pen is 10 and the price of a pencil is 5. +- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils. +- If you buy 1 pen, you can buy 0, 1, or 2 pencils. +- If you buy 2 pens, you cannot buy any pencils. +The total number of ways to buy pens and pencils is 5 + 3 + 1 = 9. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Number of Ways to Buy Pens and Pencils -```go ``` - +Input: total = 5, cost1 = 10, cost2 = 10 +Output: 1 +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. +``` ## 结语 diff --git a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution.go b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution.go index d115ccf5e..f8fa34259 100644 --- a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution.go +++ b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution.go @@ -1,5 +1,11 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(total int, cost1 int, cost2 int) int64 { + var ret int64 + penCount := 0 + for ; penCount*cost1 <= total; penCount++ { + left := (total-penCount*cost1)/cost2 + 1 // 0 + ret += int64(left) + } + return ret } diff --git a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution_test.go b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution_test.go index 14ff50eb4..9c0010fc2 100644 --- a/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution_test.go +++ b/leetcode/2201-2300/2240.Number-of-Ways-to-Buy-Pens-and-Pencils/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + total, cost1, cost2 int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 20, 10, 5, 9}, + {"TestCase2", 5, 10, 10, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.total, c.cost1, c.cost2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.total, c.cost1, c.cost2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }