Skip to content

Commit 6bfddec

Browse files
chore: add LeetCode daily solution
1 parent d7817a3 commit 6bfddec

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Number of Smooth Descent Periods of a Stock (Medium)
2+
3+
**Problem ID:** 2110
4+
**Date:** 2025-12-15
5+
**Link:** https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/
6+
7+
## Approach
8+
9+
To solve the problem of counting the number of smooth descent periods in a stock price history, we can utilize a straightforward approach that leverages a single pass through the array of prices.
10+
11+
### Main Idea:
12+
The key observation is that a smooth descent period consists of contiguous days where each day’s price is exactly 1 less than the previous day’s price. We can keep track of these periods by maintaining a count of how many consecutive days form a smooth descent.
13+
14+
### Approach:
15+
1. **Initialization**: Start by initializing a variable to store the total count of smooth descent periods (`total_periods`) and another variable to keep track of the current length of a smooth descent period (`current_length`).
16+
17+
2. **Iterate through the prices**: Loop through the `prices` array starting from the second element:
18+
- If the current price is exactly 1 less than the previous price, it indicates that we are in a smooth descent. In this case, increment the `current_length` by 1.
19+
- If the condition is not satisfied (i.e., the current price is not 1 less than the previous price), reset `current_length` to 0 since the smooth descent has ended.
20+
21+
3. **Count periods**: For each day, whether it starts a new descent or continues an existing one, add the count of smooth descent periods to `total_periods`. Each single day is also considered a valid smooth descent period, so we can add `current_length + 1` to the total periods at each step.
22+
23+
4. **Final Count**: After iterating through the entire array, the `total_periods` will contain the total count of all smooth descent periods.
24+
25+
### Data Structures:
26+
- We primarily use simple integer variables for counting (`total_periods` and `current_length`), which makes the space complexity O(1).
27+
28+
### Complexity:
29+
- **Time Complexity**: O(n), where n is the length of the `prices` array, since we are making a single pass through the array.
30+
- **Space Complexity**: O(1), as we are using a constant amount of extra space.
31+
32+
This efficient approach ensures that we can handle the upper constraint of the problem (up to 100,000 prices) comfortably within time limits.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long getDescentPeriods(int[] prices) {
3+
long count = 0;
4+
int n = prices.length;
5+
int currentPeriod = 1;
6+
7+
for (int i = 1; i < n; i++) {
8+
if (prices[i] == prices[i - 1] - 1) {
9+
currentPeriod++;
10+
} else {
11+
currentPeriod = 1;
12+
}
13+
count += currentPeriod;
14+
}
15+
16+
return count + n; // Add n for the single-day periods
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var getDescentPeriods = function(prices) {
2+
let count = 0;
3+
let currentPeriod = 1;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
if (prices[i] === prices[i - 1] - 1) {
7+
currentPeriod++;
8+
} else {
9+
currentPeriod = 1;
10+
}
11+
count += currentPeriod;
12+
}
13+
14+
return count + prices.length; // Add the individual days
15+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def getDescentPeriods(self, prices: List[int]) -> int:
3+
total_periods = len(prices)
4+
current_period = 0
5+
6+
for i in range(1, len(prices)):
7+
if prices[i] == prices[i - 1] - 1:
8+
current_period += 1
9+
else:
10+
current_period = 0
11+
total_periods += current_period
12+
13+
return total_periods

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
305305
- 2025-12-12 — [Count Mentions Per User](https://leetcode.com/problems/count-mentions-per-user/) (Medium) → `Medium/2025-12-12-3433-Count-Mentions-Per-User`
306306
- 2025-12-13 — [Coupon Code Validator](https://leetcode.com/problems/coupon-code-validator/) (Easy) → `Easy/2025-12-13-3606-Coupon-Code-Validator`
307307
- 2025-12-14 — [Number of Ways to Divide a Long Corridor](https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/) (Hard) → `Hard/2025-12-14-2147-Number-of-Ways-to-Divide-a-Long-Corridor`
308+
- 2025-12-15 — [Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/) (Medium) → `Medium/2025-12-15-2110-Number-of-Smooth-Descent-Periods-of-a-Stock`

0 commit comments

Comments
 (0)