Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 9a8556c

Browse files
aQuaaQua
aQua
authored and
aQua
committed
891 added
1 parent f6e8af5 commit 9a8556c

File tree

4 files changed

+176
-52
lines changed

4 files changed

+176
-52
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [891. Sum of Subsequence Widths](https://leetcode.com/problems/sum-of-subsequence-widths/)
2+
3+
## 题目
4+
5+
Given an array of integers A, consider all non-empty subsequences of A.
6+
7+
For any sequence S, let thewidthof S be the difference between the maximum and minimum element of S.
8+
9+
Return the sum of the widths of all subsequences of A.
10+
11+
As the answer may be very large, return the answer modulo 10^9 + 7.
12+
13+
Example 1:
14+
15+
```text
16+
Input: [2,1,3]
17+
Output: 6
18+
Explanation:
19+
Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
20+
The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
21+
The sum of these widths is 6.
22+
```
23+
24+
Note:
25+
26+
1. 1 <= A.length <= 20000
27+
1. 1 <= A[i] <= 20000
28+
29+
## 解题思路
30+
31+
见程序注释
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem0891
2+
3+
func sumSubseqWidths(A []int) int {
4+
5+
return 0
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem0891
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
A []int
13+
ans int
14+
}{
15+
16+
{
17+
[]int{2, 1, 3},
18+
6,
19+
},
20+
21+
// 可以有多个 testcase
22+
}
23+
24+
func Test_sumSubseqWidths(t *testing.T) {
25+
ast := assert.New(t)
26+
27+
for _, tc := range tcs {
28+
fmt.Printf("~~%v~~\n", tc)
29+
ast.Equal(tc.ans, sumSubseqWidths(tc.A), "输入:%v", tc)
30+
}
31+
}
32+
33+
func Benchmark_sumSubseqWidths(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
for _, tc := range tcs {
36+
sumSubseqWidths(tc.A)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)