Skip to content

Add solution and test-cases for problem 2999 #1172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading