题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一个数组代表股票,定义平滑下跌为一日值为前一日减一,求有多少个连续子数组是满足平滑下跌的。(包括长度为 1)

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def getDescentPeriods(self, prices: List[int]) -> int:
ans = 0
dec = 0
last_price = prices[0]+1
for _, p in enumerate(prices):
if last_price == p + 1:
dec += 1
else:
dec = 1
last_price = p
ans += dec
return ans