Tap here to switch tabs
Problem
Submissions

Problem: Valid Subarrays With Exactly One Peak

med
30 min
Try to solve the Valid Subarrays With Exactly One Peak problem.

Statement

You are given an integer array nums of length nn and an integer k.

An index p (where 0<p<n−10 < p < n - 1) is considered a peak if nums[p] > nums[p - 1] and nums[p] > nums[p + 1].

A subarray starting at index l and ending at index r (where 0≤l≤r<n0 \leq l \leq r < n) is considered valid if:

  1. It contains exactly 11 peak index p.

  2. The distance from the peak p to each boundary is at most k, meaning p−l≤kp - l \leq k and r−p≤kr - p \leq k.

Return the total number of valid subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • −105≤-10^5 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq k ≤n\leq n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Valid Subarrays With Exactly One Peak

med
30 min
Try to solve the Valid Subarrays With Exactly One Peak problem.

Statement

You are given an integer array nums of length nn and an integer k.

An index p (where 0<p<n−10 < p < n - 1) is considered a peak if nums[p] > nums[p - 1] and nums[p] > nums[p + 1].

A subarray starting at index l and ending at index r (where 0≤l≤r<n0 \leq l \leq r < n) is considered valid if:

  1. It contains exactly 11 peak index p.

  2. The distance from the peak p to each boundary is at most k, meaning p−l≤kp - l \leq k and r−p≤kr - p \leq k.

Return the total number of valid subarrays.

Constraints:

  • 1≤n==1 \leq n == nums.length ≤105\leq 10^5

  • −105≤-10^5 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq k ≤n\leq n