Leetcode 2316 Solution
This article provides solution to leetcode question 2316 (count-hills-and-valleys-in-an-array)
Access this page by simply typing in "lcs 2316" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/count-hills-and-valleys-in-an-array
Solution
class Solution:
def countHillValley(self, nums: List[int]) -> int:
direction = 0
curr_cnt = 0
next_direction = 0
ans = 0
for i in range(len(nums) - 1):
if nums[i] < nums[i + 1]:
next_direction = 1
elif nums[i] > nums[i + 1]:
next_direction = -1
if direction * next_direction == -1:
ans += 1
direction = next_direction
return ans