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
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -174,6 +174,50 @@ function getDescentPeriods(prices: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> 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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ Note that [8,6] is not a smooth descent period as 8 - 6 &ne; 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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -175,6 +175,50 @@ function getDescentPeriods(prices: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> 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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> 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
}
}
Loading