Skip to content

Practice Count Hills and Valleys in an Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2210
LeetCode Problem

Count Hills and Valleys in an Array

Problem Statement

You are given a 0-indexed integer array nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j]. Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index. Return the number of hills and valleys in nums.

Source: Count Hills and Valleys in an Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,4,1,1,6,5]
Output
3

# Example 2

Input
nums = [6,6,5,5,4,1]
Output
0

Constraints

3 <= nums.length <= 100
1 <= nums[i] <= 100

Related LeetCode Problems

Practice more problems similar to Count Hills and Valleys in an Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.