Skip to content

Commit ce910cb

Browse files
authored
Merge pull request 6boris#1289 from 0xff-dev/2110
Add solution and test-cases for problem 2110
2 parents c46e962 + b821992 commit ce910cb

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

leetcode/2101-2200/2110.Number-of-Smooth-Descent-Periods-of-a-Stock/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [2110.Number of Smooth Descent Periods of a Stock][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `prices` representing the daily price history of a stock, where `prices[i]` is the stock price on the `ith` day.
5+
6+
A **smooth descent period** of a stock consists of **one or more contiguous** days such that the price on each day is **lower** than the price on the **preceding day** by **exactly** `1`. The first day of the period is exempted from this rule.
7+
8+
Return the number of **smooth descent periods**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: prices = [3,2,1,4]
14+
Output: 7
15+
Explanation: There are 7 smooth descent periods:
16+
[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
17+
Note that a period with one day is a smooth descent period by the definition.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Number of Smooth Descent Periods of a Stock
23-
```go
2422
```
23+
Input: prices = [8,6,7,7]
24+
Output: 4
25+
Explanation: There are 4 smooth descent periods: [8], [6], [7], and [7]
26+
Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1.
27+
```
28+
29+
**Example 3:**
2530

31+
```
32+
Input: prices = [1]
33+
Output: 1
34+
Explanation: There is 1 smooth descent period: [1]
35+
```
2636

2737
## 结语
2838

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(prices []int) int64 {
4+
l := len(prices)
5+
c := int64(1)
6+
ans := c
7+
for i := 1; i < l; i++ {
8+
if prices[i] == prices[i-1]-1 {
9+
c++
10+
} else {
11+
c = 1
12+
}
13+
ans += c
14+
}
15+
return ans
516
}

leetcode/2101-2200/2110.Number-of-Smooth-Descent-Periods-of-a-Stock/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 2, 1, 4}, 7},
17+
{"TestCase2", []int{8, 6, 7, 7}, 4},
18+
{"TestCase3", []int{1}, 1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)