diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/README.md b/solution/2100-2199/2145.Count the Hidden Sequences/README.md index 8b541d7d3f821..2be5d6da52cf9 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/README.md +++ b/solution/2100-2199/2145.Count the Hidden Sequences/README.md @@ -85,7 +85,13 @@ tags: -### 方法一 +### 方法一:前缀和 + +由于数组 $\textit{differences}$ 已经确定,那么数组 $\textit{hidden}$ 的元素最大值与最小值之差也是固定的,我们只要确保差值不超过 $\textit{upper} - \textit{lower}$ 即可。 + +我们不妨假设数组 $\textit{hidden}$ 的第一个元素为 $0$,那么 $\textit{hidden}[i] = \textit{hidden}[i - 1] + \textit{differences}[i - 1]$,其中 $1 \leq i \leq n$。记数组 $\textit{hidden}$ 的最大值为 $mx$,最小值为 $mi$,如果 $mx - mi \leq \textit{upper} - \textit{lower}$,那么我们就可以构造出一个合法的 $\textit{hidden}$ 数组,可以构造的个数为 $\textit{upper} - \textit{lower} - (mx - mi) + 1$。否则,无法构造出合法的 $\textit{hidden}$ 数组,返回 $0$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{differences}$ 的长度。空间复杂度 $O(1)$。 @@ -94,12 +100,12 @@ tags: ```python class Solution: def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int: - num = mi = mx = 0 + x = mi = mx = 0 for d in differences: - num += d - mi = min(mi, num) - mx = max(mx, num) - return max(0, upper - lower - (mx - mi) + 1) + x += d + mi = min(mi, x) + mx = max(mx, x) + return max(upper - lower - (mx - mi) + 1, 0) ``` #### Java @@ -107,13 +113,13 @@ class Solution: ```java class Solution { public int numberOfArrays(int[] differences, int lower, int upper) { - long num = 0, mi = 0, mx = 0; + long x = 0, mi = 0, mx = 0; for (int d : differences) { - num += d; - mi = Math.min(mi, num); - mx = Math.max(mx, num); + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - return Math.max(0, (int) (upper - lower - (mx - mi) + 1)); + return (int) Math.max(upper - lower - (mx - mi) + 1, 0); } } ``` @@ -124,13 +130,13 @@ class Solution { class Solution { public: int numberOfArrays(vector& differences, int lower, int upper) { - long long num = 0, mi = 0, mx = 0; - for (int& d : differences) { - num += d; - mi = min(mi, num); - mx = max(mx, num); + long long x = 0, mi = 0, mx = 0; + for (int d : differences) { + x += d; + mi = min(mi, x); + mx = max(mx, x); } - return max(0, (int) (upper - lower - (mx - mi) + 1)); + return max(upper - lower - (mx - mi) + 1, 0LL); } }; ``` @@ -139,16 +145,30 @@ public: ```go func numberOfArrays(differences []int, lower int, upper int) int { - num, mi, mx := 0, 0, 0 + x, mi, mx := 0, 0, 0 for _, d := range differences { - num += d - mi = min(mi, num) - mx = max(mx, num) + x += d + mi = min(mi, x) + mx = max(mx, x) } return max(0, upper-lower-(mx-mi)+1) } ``` +#### TypeScript + +```ts +function numberOfArrays(differences: number[], lower: number, upper: number): number { + let [x, mi, mx] = [0, 0, 0]; + for (const d of differences) { + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return Math.max(0, upper - lower - (mx - mi) + 1); +} +``` + diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md b/solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md index 8e2df29176083..31291fa2485ab 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md +++ b/solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md @@ -86,7 +86,13 @@ Thus, we return 4. -### Solution 1 +### Solution 1: Prefix Sum + +Since the array $\textit{differences}$ is already determined, the difference between the maximum and minimum values of the elements in the array $\textit{hidden}$ is also fixed. We just need to ensure that this difference does not exceed $\textit{upper} - \textit{lower}$. + +Let's assume the first element of the array $\textit{hidden}$ is $0$. Then, $\textit{hidden}[i] = \textit{hidden}[i - 1] + \textit{differences}[i - 1]$, where $1 \leq i \leq n$. Let the maximum value of the array $\textit{hidden}$ be $mx$ and the minimum value be $mi$. If $mx - mi \leq \textit{upper} - \textit{lower}$, then we can construct a valid $\textit{hidden}$ array. The number of possible constructions is $\textit{upper} - \textit{lower} - (mx - mi) + 1$. Otherwise, it is impossible to construct a valid $\textit{hidden}$ array, and we return $0$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{differences}$. The space complexity is $O(1)$. @@ -95,12 +101,12 @@ Thus, we return 4. ```python class Solution: def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int: - num = mi = mx = 0 + x = mi = mx = 0 for d in differences: - num += d - mi = min(mi, num) - mx = max(mx, num) - return max(0, upper - lower - (mx - mi) + 1) + x += d + mi = min(mi, x) + mx = max(mx, x) + return max(upper - lower - (mx - mi) + 1, 0) ``` #### Java @@ -108,13 +114,13 @@ class Solution: ```java class Solution { public int numberOfArrays(int[] differences, int lower, int upper) { - long num = 0, mi = 0, mx = 0; + long x = 0, mi = 0, mx = 0; for (int d : differences) { - num += d; - mi = Math.min(mi, num); - mx = Math.max(mx, num); + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - return Math.max(0, (int) (upper - lower - (mx - mi) + 1)); + return (int) Math.max(upper - lower - (mx - mi) + 1, 0); } } ``` @@ -125,13 +131,13 @@ class Solution { class Solution { public: int numberOfArrays(vector& differences, int lower, int upper) { - long long num = 0, mi = 0, mx = 0; - for (int& d : differences) { - num += d; - mi = min(mi, num); - mx = max(mx, num); + long long x = 0, mi = 0, mx = 0; + for (int d : differences) { + x += d; + mi = min(mi, x); + mx = max(mx, x); } - return max(0, (int) (upper - lower - (mx - mi) + 1)); + return max(upper - lower - (mx - mi) + 1, 0LL); } }; ``` @@ -140,16 +146,30 @@ public: ```go func numberOfArrays(differences []int, lower int, upper int) int { - num, mi, mx := 0, 0, 0 + x, mi, mx := 0, 0, 0 for _, d := range differences { - num += d - mi = min(mi, num) - mx = max(mx, num) + x += d + mi = min(mi, x) + mx = max(mx, x) } return max(0, upper-lower-(mx-mi)+1) } ``` +#### TypeScript + +```ts +function numberOfArrays(differences: number[], lower: number, upper: number): number { + let [x, mi, mx] = [0, 0, 0]; + for (const d of differences) { + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return Math.max(0, upper - lower - (mx - mi) + 1); +} +``` + diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp index 65e47f76f4e27..5b4ec2d9c1c93 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp @@ -1,12 +1,12 @@ class Solution { public: int numberOfArrays(vector& differences, int lower, int upper) { - long long num = 0, mi = 0, mx = 0; - for (int& d : differences) { - num += d; - mi = min(mi, num); - mx = max(mx, num); + long long x = 0, mi = 0, mx = 0; + for (int d : differences) { + x += d; + mi = min(mi, x); + mx = max(mx, x); } - return max(0, (int) (upper - lower - (mx - mi) + 1)); + return max(upper - lower - (mx - mi) + 1, 0LL); } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.go b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.go index d0c70deb14d8b..4b2de057c8a62 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.go +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.go @@ -1,9 +1,9 @@ func numberOfArrays(differences []int, lower int, upper int) int { - num, mi, mx := 0, 0, 0 + x, mi, mx := 0, 0, 0 for _, d := range differences { - num += d - mi = min(mi, num) - mx = max(mx, num) + x += d + mi = min(mi, x) + mx = max(mx, x) } return max(0, upper-lower-(mx-mi)+1) -} \ No newline at end of file +} diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.java b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.java index 2d9d76b5b5c76..58332b21410b4 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.java +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.java @@ -1,11 +1,11 @@ class Solution { public int numberOfArrays(int[] differences, int lower, int upper) { - long num = 0, mi = 0, mx = 0; + long x = 0, mi = 0, mx = 0; for (int d : differences) { - num += d; - mi = Math.min(mi, num); - mx = Math.max(mx, num); + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); } - return Math.max(0, (int) (upper - lower - (mx - mi) + 1)); + return (int) Math.max(upper - lower - (mx - mi) + 1, 0); } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.py b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.py index ed11b3d243ca1..22ca2a7f3c11f 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.py +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.py @@ -1,8 +1,8 @@ class Solution: def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int: - num = mi = mx = 0 + x = mi = mx = 0 for d in differences: - num += d - mi = min(mi, num) - mx = max(mx, num) - return max(0, upper - lower - (mx - mi) + 1) + x += d + mi = min(mi, x) + mx = max(mx, x) + return max(upper - lower - (mx - mi) + 1, 0) diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.ts b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.ts new file mode 100644 index 0000000000000..70779fbe3f921 --- /dev/null +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.ts @@ -0,0 +1,9 @@ +function numberOfArrays(differences: number[], lower: number, upper: number): number { + let [x, mi, mx] = [0, 0, 0]; + for (const d of differences) { + x += d; + mi = Math.min(mi, x); + mx = Math.max(mx, x); + } + return Math.max(0, upper - lower - (mx - mi) + 1); +}