Count Hills and Valleys in an Array LeetCode solution in Python

1
class Solution:
2
def countHillValley(self, nums: List[int]) -> int:
3
c = 0
4
i = 1
5
while i < len(nums) - 1:
6
j = i + 1
7
while j < len(nums) - 1 and nums[j] == nums[i]:
8
j += 1
9
if (nums[i - 1] > nums[i] and nums[j] > nums[i]) or (
10
nums[i - 1] < nums[i] and nums[j] < nums[i]
11
):
12
c += 1
13
i = j
14
return c

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0