Count Decreasing Windows
Given an integer array nums and a window size k, count how many contiguous subarrays of length k are strictly decreasing. A window is strictly decreasing if each element is less than the previous element.
Check the first k elements. Then slide the window one position at a time. When an element leaves and a new element enters, only the pair involving the entering element needs to be checked. Track the count of violated pairs within the current window. When the violation count is zero, the window is valid (strictly decreasing).
Time is O(n) with O(1) space. This is the complement of the increasing windows problem; the comparison operator is simply inverted from < to >.
Example Input & Output
Single element trivially decreasing
Empty array
Only [5,3,1] is decreasing
All increasing, none decreasing
[5,4,3],[4,3,2],[3,2,1] all decreasing
Algorithm Flow

Solution Approach
Best Answers
class Solution {
public int solution(int[] nums, int k) {
if(nums.length<k)return 0;
int v=0;for(int i=1;i<k;i++){if(nums[i]>=nums[i-1])v++;}
int r=v==0?1:0;
for(int i=k;i<nums.length;i++){
if(nums[i-k]<=nums[i-k+1])v--;
if(nums[i]>=nums[i-1])v++;
if(v==0)r++;
}return r;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
