Post

[Algorithm] Leet_1343_NumberofSubarraysofSizeKandAverageGreaterthanorEqualtoThreshold

Leet_1343_NumberofSubarraysofSizeKandAverageGreaterthanorEqualtoThreshold 접근방식

[Algorithm] Leet_1343_NumberofSubarraysofSizeKandAverageGreaterthanorEqualtoThreshold

Leet_1343_NumberofSubarraysofSizeKandAverageGreaterthanorEqualtoThreshold

문제 링크

https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/

카테고리

슬라이딩 윈도우

접근 방식

일반적인 고정된 슬라이딩 윈도우 문제이기에 비슷한 방법으로 풀었다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package ver2.Leet_1343_NumberofSubarraysofSizeKandAverageGreaterthanorEqualtoThreshold;

class Solution {
    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int i = 0;
        int j = 0;
        int n = arr.length;
        int ans = 0;
        int sum = 0;

        while(j < n){
            if(j - i + 1 < k){
                sum += arr[j];
                j++;
            }
            else{
                sum += arr[j];

                if(sum / k >= threshold) ans++;

                sum -= arr[i];

                i++;
                j++;
            }
        }
        return ans;
    }
}
This post is licensed under CC BY 4.0 by the author.