1343.number-of-avg-subarr-sizek-greater-or-equal-threshold
Problem
1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
Problem Description
Solution
This problem is pretty straigtforward, group each size k subarray, calculate avg of this group, compare avg and threshold.
Steps:
check whether current length of arr and size k
if len < k, return false. not enough elements to from a group subarray of size k.
scan arr from start position 0, each time check group a subarray of size k.
calculate avg of subarray of current size k [i, i + k -1],
if avg >= threshold, count + 1
otherwise, do nothing, continue, index i+1;
until last pos (len - k). return count.
For example:
Complexity Analysis
Time Complexity:
O(n) - n is the length of arr
Space Complexity:
O(1) - no extra space
Key Points
Group each size k subarray.
Calculate avg of subarray of size k, compare with threshold
Count increase 1 if meet requirements
Code
Java Code
Last updated