Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
# [2145.Count the Hidden Sequences][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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]`.

You are further given two integers `lower` and `upper` that describe the **inclusive** range of values `[lower, upper]` that the hidden sequence can contain.

- 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**).

- `[3, 4, 1, 5]` and `[4, 5, 2, 6]` are possible hidden sequences.
- `[5, 6, 3, 7]` is not possible since it contains an element greater than `6`.
- `[1, 2, 3, 4]` is not possible since the differences are not correct.

Return the number of **possible** hidden sequences there are. If there are no possible sequences, return `0`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: differences = [1,-3,4], lower = 1, upper = 6
Output: 2
Explanation: The possible hidden sequences are:
- [3, 4, 1, 5]
- [4, 5, 2, 6]
Thus, we return 2.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Count the Hidden Sequences
```go
```
Input: differences = [3,-4,5,1,-2], lower = -4, upper = 5
Output: 4
Explanation: The possible hidden sequences are:
- [-3, 0, -4, 1, 2, 0]
- [-2, 1, -3, 2, 3, 1]
- [-1, 2, -2, 3, 4, 2]
- [0, 3, -1, 4, 5, 3]
Thus, we return 4.
```

**Example 3:**

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

## 结语

Expand Down
24 changes: 22 additions & 2 deletions leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
import "math"

func Solution(differences []int, lower int, upper int) int {
sum := int64(0)
var maxSum, minSum int64
maxSum, minSum = int64(0), math.MaxInt64
for _, n := range differences {
sum += int64(n)
maxSum = max(sum, maxSum)
minSum = min(sum, minSum)
}

ans := 0
il, ip := int64(lower), int64(upper)
for i := lower; i <= upper; i++ {
a := int64(i) + maxSum
b := int64(i) + minSum
if a >= il && a <= ip && b >= il && b <= ip {
ans++
}
}
return ans
}
23 changes: 12 additions & 11 deletions leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
differences []int
lower, upper int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, -3, 4}, 1, 6, 2},
{"TestCase2", []int{3, -4, 5, 1, -2}, -4, 5, 4},
{"TestCase3", []int{4, -7, 2}, 3, 6, 0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.differences, c.lower, c.upper)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.differences, c.lower, c.upper)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading