Skip to content

Commit 2286718

Browse files
authored
Merge pull request #1182 from 0xff-dev/2145
Add solution and test-cases for problem 2145
2 parents d0b1f79 + ff5f109 commit 2286718

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# [2145.Count the Hidden Sequences][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 a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the `hidden` sequence hidden, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.
5+
6+
You are further given two integers `lower` and `upper` that describe the **inclusive** range of values `[lower, upper]` that the hidden sequence can contain.
7+
8+
- For example, given `differences = [1, -3, 4]`, `lower = 1`, `upper = 6`, the hidden sequence is a sequence of length `4` whose elements are in between `1` and `6` (**inclusive**).
9+
10+
- `[3, 4, 1, 5]` and `[4, 5, 2, 6]` are possible hidden sequences.
11+
- `[5, 6, 3, 7]` is not possible since it contains an element greater than `6`.
12+
- `[1, 2, 3, 4]` is not possible since the differences are not correct.
13+
14+
Return the number of **possible** hidden sequences there are. If there are no possible sequences, return `0`.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: differences = [1,-3,4], lower = 1, upper = 6
20+
Output: 2
21+
Explanation: The possible hidden sequences are:
22+
- [3, 4, 1, 5]
23+
- [4, 5, 2, 6]
24+
Thus, we return 2.
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count the Hidden Sequences
23-
```go
2429
```
30+
Input: differences = [3,-4,5,1,-2], lower = -4, upper = 5
31+
Output: 4
32+
Explanation: The possible hidden sequences are:
33+
- [-3, 0, -4, 1, 2, 0]
34+
- [-2, 1, -3, 2, 3, 1]
35+
- [-1, 2, -2, 3, 4, 2]
36+
- [0, 3, -1, 4, 5, 3]
37+
Thus, we return 4.
38+
```
39+
40+
**Example 3:**
2541

42+
```
43+
Input: differences = [4,-7,2], lower = 3, upper = 6
44+
Output: 0
45+
Explanation: There are no possible hidden sequences. Thus, we return 0.
46+
```
2647

2748
## 结语
2849

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func Solution(differences []int, lower int, upper int) int {
6+
sum := int64(0)
7+
var maxSum, minSum int64
8+
maxSum, minSum = int64(0), math.MaxInt64
9+
for _, n := range differences {
10+
sum += int64(n)
11+
maxSum = max(sum, maxSum)
12+
minSum = min(sum, minSum)
13+
}
14+
15+
ans := 0
16+
il, ip := int64(lower), int64(upper)
17+
for i := lower; i <= upper; i++ {
18+
a := int64(i) + maxSum
19+
b := int64(i) + minSum
20+
if a >= il && a <= ip && b >= il && b <= ip {
21+
ans++
22+
}
23+
}
24+
return ans
525
}

leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go

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

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.differences, c.lower, c.upper)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.differences, c.lower, c.upper)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)