Skip to content

Commit 21a6b29

Browse files
authored
feat: add solutions to lc problem: No.2110 (#4905)
1 parent 2316b01 commit 21a6b29

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed

solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ tags:
6969

7070
### 方法一:双指针
7171

72-
我们定义一个答案变量 `ans`,初始值为 $0$。
72+
我们定义一个答案变量 $\textit{ans}$,初始值为 $0$。
7373

7474
接下来,我们使用双指针 $i$ 和 $j$,分别指向当前平滑下降阶段的第一天和最后一天的下一天。初始时 $i = 0$, $j = 0$。
7575

76-
从左到右遍历数组 `prices`,对于每个位置 $i$,我们将 $j$ 向右移动,直到 $j$ 到达数组末尾或者 $prices[j - 1] - prices[j] \neq 1$ 为止。此时,$cnt = j - i$ 即为当前平滑下降阶段的长度,我们累加 $\frac{(1 + cnt) \times cnt}{2}$ 到答案变量 `ans` 中。接下来将 $i$ 更新为 $j$,继续遍历。
76+
从左到右遍历数组 $\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$,继续遍历。
7777

78-
遍历结束后,返回答案变量 `ans` 即可。
78+
遍历结束后,返回答案变量 $\textit{ans}$ 即可。
7979

80-
时间复杂度 $O(n)$,其中 $n$ 为数组 `prices` 的长度。空间复杂度 $O(1)$。
80+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{prices}$ 的长度。空间复杂度 $O(1)$。
8181

8282
<!-- tabs:start -->
8383

@@ -174,6 +174,50 @@ function getDescentPeriods(prices: number[]): number {
174174
}
175175
```
176176

177+
#### Rust
178+
179+
```rust
180+
impl Solution {
181+
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
182+
let mut ans: i64 = 0;
183+
let n: usize = prices.len();
184+
let mut i: usize = 0;
185+
186+
while i < n {
187+
let mut j: usize = i + 1;
188+
while j < n && prices[j - 1] - prices[j] == 1 {
189+
j += 1;
190+
}
191+
let cnt: i64 = (j - i) as i64;
192+
ans += (1 + cnt) * cnt / 2;
193+
i = j;
194+
}
195+
196+
ans
197+
}
198+
}
199+
```
200+
201+
#### C#
202+
203+
```cs
204+
public class Solution {
205+
public long GetDescentPeriods(int[] prices) {
206+
long ans = 0;
207+
int n = prices.Length;
208+
for (int i = 0, j = 0; i < n; i = j) {
209+
j = i + 1;
210+
while (j < n && prices[j - 1] - prices[j] == 1) {
211+
++j;
212+
}
213+
int cnt = j - i;
214+
ans += (1L + cnt) * cnt / 2;
215+
}
216+
return ans;
217+
}
218+
}
219+
```
220+
177221
<!-- tabs:end -->
178222

179223
<!-- solution:end -->

solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/README_EN.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ Note that [8,6] is not a smooth descent period as 8 - 6 &ne; 1.
7070

7171
### Solution 1: Two Pointers
7272

73-
We define an answer variable `ans`, initially set to $0$.
73+
We define an answer variable $\textit{ans}$ with an initial value of $0$.
7474

75-
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$.
75+
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$.
7676

77-
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.
77+
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.
7878

79-
After the iteration ends, return the answer variable `ans`.
79+
After the traversal ends, we return the answer variable $\textit{ans}$.
8080

81-
The time complexity is $O(n)$, where $n$ is the length of the array `prices`. The space complexity is $O(1)$.
81+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{prices}$. The space complexity is $O(1)$.
8282

8383
<!-- tabs:start -->
8484

@@ -175,6 +175,50 @@ function getDescentPeriods(prices: number[]): number {
175175
}
176176
```
177177

178+
#### Rust
179+
180+
```rust
181+
impl Solution {
182+
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
183+
let mut ans: i64 = 0;
184+
let n: usize = prices.len();
185+
let mut i: usize = 0;
186+
187+
while i < n {
188+
let mut j: usize = i + 1;
189+
while j < n && prices[j - 1] - prices[j] == 1 {
190+
j += 1;
191+
}
192+
let cnt: i64 = (j - i) as i64;
193+
ans += (1 + cnt) * cnt / 2;
194+
i = j;
195+
}
196+
197+
ans
198+
}
199+
}
200+
```
201+
202+
#### C#
203+
204+
```cs
205+
public class Solution {
206+
public long GetDescentPeriods(int[] prices) {
207+
long ans = 0;
208+
int n = prices.Length;
209+
for (int i = 0, j = 0; i < n; i = j) {
210+
j = i + 1;
211+
while (j < n && prices[j - 1] - prices[j] == 1) {
212+
++j;
213+
}
214+
int cnt = j - i;
215+
ans += (1L + cnt) * cnt / 2;
216+
}
217+
return ans;
218+
}
219+
}
220+
```
221+
178222
<!-- tabs:end -->
179223

180224
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public long GetDescentPeriods(int[] prices) {
3+
long ans = 0;
4+
int n = prices.Length;
5+
for (int i = 0, j = 0; i < n; i = j) {
6+
j = i + 1;
7+
while (j < n && prices[j - 1] - prices[j] == 1) {
8+
++j;
9+
}
10+
int cnt = j - i;
11+
ans += (1L + cnt) * cnt / 2;
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
3+
let mut ans: i64 = 0;
4+
let n: usize = prices.len();
5+
let mut i: usize = 0;
6+
7+
while i < n {
8+
let mut j: usize = i + 1;
9+
while j < n && prices[j - 1] - prices[j] == 1 {
10+
j += 1;
11+
}
12+
let cnt: i64 = (j - i) as i64;
13+
ans += (1 + cnt) * cnt / 2;
14+
i = j;
15+
}
16+
17+
ans
18+
}
19+
}

0 commit comments

Comments
 (0)