Skip to content

Commit ab564f8

Browse files
authored
Merge pull request #1284 from 0xff-dev/2438
Add solution and test-cases for problem 2438
2 parents dfa1db6 + 76cf912 commit ab564f8

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

leetcode/2401-2500/2438.Range-Product-Queries-of-Powers/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [2438.Range Product Queries of Powers][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+
Given a positive integer `n`, there exists a **0-indexed** array called `powers`, composed of the **minimum** number of powers of `2` that sum to `n`. The array is sorted in **non-decreasing** order, and there is **only one** way to form the array.
5+
6+
You are also given a **0-indexed** 2D integer array `queries`, where `queries[i] = [lefti, righti]`. Each `queries[i]` represents a query where you have to find the product of all `powers[j]` with `lefti <= j <= righti`.
7+
8+
Return an array `answer`, equal in length to `queries`, where `answers[i]` is the answer to the `ith` query. Since the answer to the `ith` query may be too large, each `answers[i]` should be returned **modulo** `10^9 + 7`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: n = 15, queries = [[0,1],[2,2],[0,3]]
14+
Output: [2,4,64]
15+
Explanation:
16+
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
17+
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
18+
Answer to 2nd query: powers[2] = 4.
19+
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
20+
Each answer modulo 109 + 7 yields the same answer, so [2,4,64] is returned.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Range Product Queries of Powers
23-
```go
2425
```
25-
26+
Input: n = 2, queries = [[0,0]]
27+
Output: [2]
28+
Explanation:
29+
For n = 2, powers = [2].
30+
The answer to the only query is powers[0] = 2. The answer modulo 109 + 7 is the same, so [2] is returned.
31+
```
2632

2733
## 结语
2834

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func construct(n int, powers [33]int) []int {
6+
if n&(n-1) == 0 {
7+
return []int{n}
8+
}
9+
index := sort.Search(33, func(i int) bool {
10+
return powers[i] > n
11+
})
12+
set := []int{powers[index-1]}
13+
left := construct(n-powers[index-1], powers)
14+
set = append(set, left...)
15+
return set
16+
}
17+
18+
func modPow(x int64, n int64, m int64) int64 {
19+
result := int64(1)
20+
x %= m
21+
for n > 0 {
22+
if n&1 == 1 {
23+
result = (result * x) % m
24+
}
25+
x = (x * x) % m
26+
n >>= 1
27+
}
28+
return result
29+
}
30+
31+
func modInverse(x int64, m int64) int64 {
32+
return modPow(x, m-2, m)
33+
}
34+
35+
const mod = 1000000007
36+
37+
func Solution(n int, queries [][]int) []int {
38+
39+
powers := [33]int{}
40+
powers[0] = 1
41+
for i := 1; i < 33; i++ {
42+
powers[i] = powers[i-1] * 2
43+
}
44+
arr := construct(n, powers)
45+
for s, e := 0, len(arr)-1; s < e; s, e = s+1, e-1 {
46+
arr[s], arr[e] = arr[e], arr[s]
47+
}
48+
ans := make([]int, len(queries))
49+
prefix := make([]int64, len(arr)+1)
50+
index := 0
51+
prefix[index] = 1
52+
for i := 0; i < len(arr); i++ {
53+
prefix[i+1] = (prefix[index] * int64(arr[i])) % mod
54+
index++
55+
}
56+
for i, q := range queries {
57+
l, r := q[0], q[1]
58+
res := (prefix[r+1] * modInverse(prefix[l], mod)) % mod
59+
ans[i] = int(res)
60+
}
61+
return ans
562
}

leetcode/2401-2500/2438.Range-Product-Queries-of-Powers/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 15, [][]int{{0, 1}, {2, 2}, {0, 3}}, []int{2, 4, 64}},
18+
{"TestCase2", 2, [][]int{{0, 0}}, []int{2}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.n, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)