From 2b53b170ff345860e9893145f9cbe5e606198f40 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 15 Dec 2025 07:42:14 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2110 --- .../README.md | 52 ++++++++++++++++-- .../README_EN.md | 54 +++++++++++++++++-- .../Solution.cs | 15 ++++++ .../Solution.rs | 19 +++++++ 4 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cs create mode 100644 solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.rs diff --git a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README.md b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README.md index e60eff886c505..4e523d69d5a57 100644 --- a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README.md +++ b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README.md @@ -69,15 +69,15 @@ tags: ### 方法一:双指针 -我们定义一个答案变量 `ans`,初始值为 $0$。 +我们定义一个答案变量 $\textit{ans}$,初始值为 $0$。 接下来,我们使用双指针 $i$ 和 $j$,分别指向当前平滑下降阶段的第一天和最后一天的下一天。初始时 $i = 0$, $j = 0$。 -从左到右遍历数组 `prices`,对于每个位置 $i$,我们将 $j$ 向右移动,直到 $j$ 到达数组末尾或者 $prices[j - 1] - prices[j] \neq 1$ 为止。此时,$cnt = j - i$ 即为当前平滑下降阶段的长度,我们累加 $\frac{(1 + cnt) \times cnt}{2}$ 到答案变量 `ans` 中。接下来将 $i$ 更新为 $j$,继续遍历。 +从左到右遍历数组 $\textit{prices}$,对于每个位置 $i$,我们将 $j$ 向右移动,直到 $j$ 到达数组末尾或者 $\textit{prices}[j - 1] - \textit{prices}[j] \neq 1$ 为止。此时,$\textit{cnt} = j - i$ 即为当前平滑下降阶段的长度,我们累加 $\frac{(1 + \textit{cnt}) \times \textit{cnt}}{2}$ 到答案变量 $\textit{ans}$ 中。接下来将 $i$ 更新为 $j$,继续遍历。 -遍历结束后,返回答案变量 `ans` 即可。 +遍历结束后,返回答案变量 $\textit{ans}$ 即可。 -时间复杂度 $O(n)$,其中 $n$ 为数组 `prices` 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{prices}$ 的长度。空间复杂度 $O(1)$。 @@ -174,6 +174,50 @@ function getDescentPeriods(prices: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_descent_periods(prices: Vec) -> i64 { + let mut ans: i64 = 0; + let n: usize = prices.len(); + let mut i: usize = 0; + + while i < n { + let mut j: usize = i + 1; + while j < n && prices[j - 1] - prices[j] == 1 { + j += 1; + } + let cnt: i64 = (j - i) as i64; + ans += (1 + cnt) * cnt / 2; + i = j; + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public long GetDescentPeriods(int[] prices) { + long ans = 0; + int n = prices.Length; + for (int i = 0, j = 0; i < n; i = j) { + j = i + 1; + while (j < n && prices[j - 1] - prices[j] == 1) { + ++j; + } + int cnt = j - i; + ans += (1L + cnt) * cnt / 2; + } + return ans; + } +} +``` + diff --git a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README_EN.md b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README_EN.md index d1614b9154fef..1de403e61f5a0 100644 --- a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README_EN.md +++ b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README_EN.md @@ -70,15 +70,15 @@ Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1. ### Solution 1: Two Pointers -We define an answer variable `ans`, initially set to $0$. +We define an answer variable $\textit{ans}$ with an initial value of $0$. -Next, we use two pointers $i$ and $j$, pointing to the first day of the current smooth decline phase and the day after the last day of this phase, respectively. Initially, $i = 0$, $j = 0$. +Next, we use two pointers $i$ and $j$, which point to the first day of the current smooth descent period and the day after the last day, respectively. Initially, $i = 0$ and $j = 0$. -Iterate through the array `prices` from left to right. For each position $i$, we move $j$ to the right until $j$ reaches the end of the array or $prices[j - 1] - prices[j] \neq 1$. At this point, $cnt = j - i$ is the length of the current smooth decline phase, and we add $\frac{(1 + cnt) \times cnt}{2}$ to the answer variable `ans`. Then, we update $i$ to $j$ and continue the iteration. +We traverse the array $\textit{prices}$ from left to right. For each position $i$, we move $j$ to the right until $j$ reaches the end of the array or $\textit{prices}[j - 1] - \textit{prices}[j] \neq 1$. At this point, $\textit{cnt} = j - i$ is the length of the current smooth descent period, and we add $\frac{(1 + \textit{cnt}) \times \textit{cnt}}{2}$ to the answer variable $\textit{ans}$. Then we update $i$ to $j$ and continue traversing. -After the iteration ends, return the answer variable `ans`. +After the traversal ends, we return the answer variable $\textit{ans}$. -The time complexity is $O(n)$, where $n$ is the length of the array `prices`. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{prices}$. The space complexity is $O(1)$. @@ -175,6 +175,50 @@ function getDescentPeriods(prices: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_descent_periods(prices: Vec) -> i64 { + let mut ans: i64 = 0; + let n: usize = prices.len(); + let mut i: usize = 0; + + while i < n { + let mut j: usize = i + 1; + while j < n && prices[j - 1] - prices[j] == 1 { + j += 1; + } + let cnt: i64 = (j - i) as i64; + ans += (1 + cnt) * cnt / 2; + i = j; + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public long GetDescentPeriods(int[] prices) { + long ans = 0; + int n = prices.Length; + for (int i = 0, j = 0; i < n; i = j) { + j = i + 1; + while (j < n && prices[j - 1] - prices[j] == 1) { + ++j; + } + int cnt = j - i; + ans += (1L + cnt) * cnt / 2; + } + return ans; + } +} +``` + diff --git a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cs b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cs new file mode 100644 index 0000000000000..92c76e6a4e3c0 --- /dev/null +++ b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cs @@ -0,0 +1,15 @@ +public class Solution { + public long GetDescentPeriods(int[] prices) { + long ans = 0; + int n = prices.Length; + for (int i = 0, j = 0; i < n; i = j) { + j = i + 1; + while (j < n && prices[j - 1] - prices[j] == 1) { + ++j; + } + int cnt = j - i; + ans += (1L + cnt) * cnt / 2; + } + return ans; + } +} diff --git a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.rs b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.rs new file mode 100644 index 0000000000000..ad9162836f9a5 --- /dev/null +++ b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn get_descent_periods(prices: Vec) -> i64 { + let mut ans: i64 = 0; + let n: usize = prices.len(); + let mut i: usize = 0; + + while i < n { + let mut j: usize = i + 1; + while j < n && prices[j - 1] - prices[j] == 1 { + j += 1; + } + let cnt: i64 = (j - i) as i64; + ans += (1 + cnt) * cnt / 2; + i = j; + } + + ans + } +}