From e51d3816f4952ef7021aa12908085ab01ae3c8c5 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 10 Apr 2025 22:43:22 +0800 Subject: [PATCH] Add solution and test-cases for problem 2999 --- .../README.md | 37 ++++++++----- .../Solution.go | 54 ++++++++++++++++++- .../Solution_test.go | 24 +++++---- 3 files changed, 89 insertions(+), 26 deletions(-) diff --git a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/README.md b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/README.md index d3f7f55d0..5e06981df 100755 --- a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/README.md +++ b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/README.md @@ -1,28 +1,39 @@ # [2999.Count the Number of Powerful Integers][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 three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer. + +A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`. + +Return the **total** number of powerful integers in the range `[start..finish]`. + +A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including** `0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: start = 1, finish = 6000, limit = 4, s = "124" +Output: 5 +Explanation: The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4. +It can be shown that there are only 5 powerful integers in this range. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Count the Number of Powerful Integers -```go ``` +Input: start = 15, finish = 215, limit = 6, s = "10" +Output: 2 +Explanation: The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix. +It can be shown that there are only 2 powerful integers in this range. +``` + +**Example 3:** +``` +Input: start = 1000, finish = 2000, limit = 4, s = "3000" +Output: 0 +Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range. +``` ## 结语 diff --git a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution.go b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution.go index d115ccf5e..89ef79084 100644 --- a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution.go +++ b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution.go @@ -1,5 +1,55 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "fmt" + "strings" +) + +func Solution(start int64, finish int64, limit int, s string) int64 { + low := fmt.Sprintf("%d", start) + high := fmt.Sprintf("%d", finish) + n := len(high) + low = strings.Repeat("0", n-len(low)) + low // align digits + pre_len := n - len(s) // prefix length + memo := make([]int64, n) + for i := range memo { + memo[i] = -1 + } + + var dfs func(int, bool, bool) int64 + dfs = func(i int, limit_low, limit_high bool) int64 { + // recursive boundary + if i == n { + return 1 + } + if !limit_low && !limit_high && memo[i] != -1 { + return memo[i] + } + lo := 0 + if limit_low { + lo = int(low[i] - '0') + } + hi := 9 + if limit_high { + hi = int(high[i] - '0') + } + + var res int64 = 0 + if i < pre_len { + for digit := lo; digit <= min(hi, limit); digit++ { + res += dfs(i+1, limit_low && digit == lo, limit_high && digit == hi) + } + } else { + x := int(s[i-pre_len] - '0') + if lo <= x && x <= min(hi, limit) { + res = dfs(i+1, limit_low && x == lo, limit_high && x == hi) + } + } + + if !limit_low && !limit_high { + memo[i] = res + } + return res + } + return dfs(0, true, true) } diff --git a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution_test.go b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution_test.go index 14ff50eb4..4c1a9aa93 100644 --- a/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution_test.go +++ b/leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution_test.go @@ -9,31 +9,33 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + start, finish int64 + limit int + s string + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 1, 6000, 4, "124", 5}, + {"TestCase2", 15, 215, 6, "10", 2}, + {"TestCase3", 1000, 2000, 4, "3000", 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.start, c.finish, c.limit, c.s) 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 %v", + c.expect, got, c.start, c.finish, c.limit, c.s) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }