2210. Count Hills and Valleys in an Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2210

class Solution:
    def countHillValley(self, nums: List[int]) -> int:
        res = j = 0
        for i in range(1, len(nums) - 1):
            if nums[i] == nums[i + 1]:
                continue
            if nums[i] > nums[j] and nums[i] > nums[i + 1]:
                res += 1
            if nums[i] < nums[j] and nums[i] < nums[i + 1]:
                res += 1
            j = i
        return res