FastPrepCount Valleys in a Wiggle Pattern

Count Valleys in a Wiggle Pattern

CoinDCX logoCoinDCX● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A valley is an interior index whose value is strictly smaller than both adjacent values.

Return the number of valleys in nums. Endpoints are never valleys, and equal neighbors do not form a strict valley.

Function

countValleys(nums: int[]) → int

Examples

Example 1

nums = [5,1,4,2,3]return = 2

Indices 1 and 3 are strict valleys.

Example 2

nums = [1,2,3]return = 0

The sequence has no valley.

Constraints

  • 0 <= nums.length <= 100000.
  • -10^9 <= nums[i] <= 10^9.

Source note: Author-written prompt excerpt retained for source fidelity.

More CoinDCX problems

See CoinDCX hiring insights
public int countValleys(int[] nums) {
    // Write your code here.
}
nums[5,1,4,2,3]
expected2
Checking account…