Skip to content

Commit 04d8268

Browse files
authored
Merge pull request #1172 from 0xff-dev/2999
Add solution and test-cases for problem 2999
2 parents 638aaab + e51d381 commit 04d8268

File tree

3 files changed

+89
-26
lines changed

3 files changed

+89
-26
lines changed

Diff for: leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/README.md

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2999.Count the Number of Powerful Integers][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
4+
You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer.
5+
6+
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`.
7+
8+
Return the **total** number of powerful integers in the range `[start..finish]`.
9+
10+
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.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: start = 1, finish = 6000, limit = 4, s = "124"
16+
Output: 5
17+
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.
18+
It can be shown that there are only 5 powerful integers in this range.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Count the Number of Powerful Integers
23-
```go
2423
```
24+
Input: start = 15, finish = 215, limit = 6, s = "10"
25+
Output: 2
26+
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.
27+
It can be shown that there are only 2 powerful integers in this range.
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: start = 1000, finish = 2000, limit = 4, s = "3000"
34+
Output: 0
35+
Explanation: All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.
36+
```
2637

2738
## 结语
2839

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func Solution(start int64, finish int64, limit int, s string) int64 {
9+
low := fmt.Sprintf("%d", start)
10+
high := fmt.Sprintf("%d", finish)
11+
n := len(high)
12+
low = strings.Repeat("0", n-len(low)) + low // align digits
13+
pre_len := n - len(s) // prefix length
14+
memo := make([]int64, n)
15+
for i := range memo {
16+
memo[i] = -1
17+
}
18+
19+
var dfs func(int, bool, bool) int64
20+
dfs = func(i int, limit_low, limit_high bool) int64 {
21+
// recursive boundary
22+
if i == n {
23+
return 1
24+
}
25+
if !limit_low && !limit_high && memo[i] != -1 {
26+
return memo[i]
27+
}
28+
lo := 0
29+
if limit_low {
30+
lo = int(low[i] - '0')
31+
}
32+
hi := 9
33+
if limit_high {
34+
hi = int(high[i] - '0')
35+
}
36+
37+
var res int64 = 0
38+
if i < pre_len {
39+
for digit := lo; digit <= min(hi, limit); digit++ {
40+
res += dfs(i+1, limit_low && digit == lo, limit_high && digit == hi)
41+
}
42+
} else {
43+
x := int(s[i-pre_len] - '0')
44+
if lo <= x && x <= min(hi, limit) {
45+
res = dfs(i+1, limit_low && x == lo, limit_high && x == hi)
46+
}
47+
}
48+
49+
if !limit_low && !limit_high {
50+
memo[i] = res
51+
}
52+
return res
53+
}
54+
return dfs(0, true, true)
555
}

Diff for: leetcode/2901-3000/2999.Count-the-Number-of-Powerful-Integers/Solution_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,33 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
start, finish int64
14+
limit int
15+
s string
16+
expect int64
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", 1, 6000, 4, "124", 5},
19+
{"TestCase2", 15, 215, 6, "10", 2},
20+
{"TestCase3", 1000, 2000, 4, "3000", 0},
1921
}
2022

2123
// 开始测试
2224
for i, c := range cases {
2325
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
26+
got := Solution(c.start, c.finish, c.limit, c.s)
2527
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
29+
c.expect, got, c.start, c.finish, c.limit, c.s)
2830
}
2931
})
3032
}
3133
}
3234

33-
// 压力测试
35+
// 压力测试
3436
func BenchmarkSolution(b *testing.B) {
3537
}
3638

37-
// 使用案列
39+
// 使用案列
3840
func ExampleSolution() {
3941
}

0 commit comments

Comments
 (0)