From ff5f109ad95123aa4e4f31af65b16ccb264ded34 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 21 Apr 2025 09:15:07 +0800 Subject: [PATCH] Add solution and test-cases for problem 2145 --- .../2145.Count-the-Hidden-Sequences/README.md | 47 ++++++++++++++----- .../Solution.go | 24 +++++++++- .../Solution_test.go | 23 ++++----- 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md index eaa01e830..4658816d3 100755 --- a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md +++ b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution.go b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution.go index d115ccf5e..5bc061d21 100644 --- a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution.go +++ b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution.go @@ -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 } diff --git a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go index 14ff50eb4..2fe381d4f 100644 --- a/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go +++ b/leetcode/2101-2200/2145.Count-the-Hidden-Sequences/Solution_test.go @@ -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() { }