Skip to content

Commit 8d541f3

Browse files
authored
Merge pull request #1285 from 0xff-dev/2787
Add solution and test-cases for problem 2787
2 parents ab564f8 + 6a53eba commit 8d541f3

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [2787.Ways to Express an Integer as Sum of Powers][title]
2+
3+
## Description
4+
Given two **positive** integers `n` and `n`.
5+
6+
Return the number of ways `n` can be expressed as the sum of the `xth` power of **unique** positive integers, in other words, the number of sets of unique integers `[n1, n2, ..., nk]` where `n = n1x + n2x + ... + nkx`.
7+
8+
Since the result can be very large, return it modulo `10^9 + 7`.
9+
10+
For example, if `n = 160` and `x = 3`, one way to express `n` is `n = 2^3 + 3^3 + 5^3`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: n = 10, x = 2
16+
Output: 1
17+
Explanation: We can express n as the following: n = 32 + 12 = 10.
18+
It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers.
19+
```
20+
21+
**EXample 2:**
22+
23+
```
24+
Input: n = 4, x = 1
25+
Output: 2
26+
Explanation: We can express n in the following ways:
27+
- n = 41 = 4.
28+
- n = 31 + 11 = 4.
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/ways-to-express-an-integer-as-sum-of-powers
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func power(base, n int) int {
4+
result := 1
5+
for i := 0; i < n; i++ {
6+
result *= base
7+
}
8+
return result
9+
}
10+
11+
const mod = 1000000007
12+
13+
func Solution(n int, x int) int {
14+
candidates := []int{}
15+
i := 1
16+
for ; ; i++ {
17+
p := power(i, x)
18+
if p > n {
19+
break
20+
}
21+
candidates = append(candidates, p)
22+
}
23+
24+
var dfs func(index, target int) int
25+
cache := make(map[[2]int]int)
26+
27+
dfs = func(index, target int) int {
28+
if target == 0 {
29+
return 1
30+
}
31+
if target < 0 || index == len(candidates) {
32+
return 0
33+
}
34+
key := [2]int{index, target}
35+
if val, ok := cache[key]; ok {
36+
return val
37+
}
38+
res := dfs(index+1, target-candidates[index]) % mod
39+
res = (res + dfs(index+1, target)) % mod
40+
cache[key] = res
41+
return res
42+
}
43+
44+
return dfs(0, n)
545
}

leetcode/2701-2800/2787.Ways-to-Express-an-Integer-as-Sum-of-Powers/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n, x int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 10, 2, 1},
17+
{"TestCase2", 4, 1, 2},
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.n, c.x)
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",
26+
c.expect, got, c.n, c.x)
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)