-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2110.py
30 lines (23 loc) · 908 Bytes
/
p2110.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import unittest
from typing import List
# 找出每一组平滑下降的子数组,其长度为n,其中平滑下降定义为后一天比前一天恰好小1
# 对于每个子数组,平滑下降阶段的数目为1+2+..+n = n*(1+n)/2
class Solution:
@staticmethod
def getDescentPeriods(prices: List[int]) -> int:
ans = 0
i = 0
while i < len(prices):
start = i
i += 1
while i < len(prices) and prices[i] - prices[i - 1] == -1:
i += 1
ans += (i - start) * (1 + (i - start)) // 2
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(Solution.getDescentPeriods([3, 2, 1, 4]), 7)
self.assertEqual(Solution.getDescentPeriods([8, 6, 7, 7]), 4)
self.assertEqual(Solution.getDescentPeriods([1]), 1)
if __name__ == "__main__":
unittest.main()