leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit d12256a3a2fc671483042d4c133f87a8c92dcec2
parent 3041c7b184c3de23da68def32aeb59f7dceac0b9
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun, 14 Dec 2025 20:35:38 -0700

2110. number of smooth descent periods of a stock

Diffstat:
Mnum_smooth_descent_periods_stock.c | 8+++-----
Mnum_smooth_descent_periods_stock.py | 9++++++---
2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/num_smooth_descent_periods_stock.c b/num_smooth_descent_periods_stock.c @@ -11,14 +11,12 @@ */ long long getDescentPeriods(int *prices, int pricesSize) { - int *dp = (int *)malloc(pricesSize * sizeof(int)); - dp[0] = 1; + int dp = 1; long long ans = 1; for (int i = 1; i < pricesSize; i++) { - dp[i] = prices[i] == prices[i - 1] - 1 ? dp[i - 1] + 1 : 1; - ans += dp[i]; + dp = prices[i] == prices[i - 1] - 1 ? dp + 1 : 1; + ans += dp; } - free(dp); return ans; } diff --git a/num_smooth_descent_periods_stock.py b/num_smooth_descent_periods_stock.py @@ -16,11 +16,14 @@ class Solution(object): :type prices: List[int] :rtype: int """ - dp = [1] * len(prices) + ans, dp = 1, 1 for i in range(1, len(prices)): if prices[i] == prices[i - 1] - 1: - dp[i] += dp[i - 1] - return sum(dp) + dp += 1 + else: + dp = 1 + ans += dp + return ans if __name__ == "__main__":