Skip to content

Commit 471510b

Browse files
add 1343
1 parent dcc27ed commit 471510b

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium|Array|
1112
|1342|[Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Easy|Bit Manipulation|
1213
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
1314
|1339|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1339.java) | |Medium|DP, Tree|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
5+
*
6+
* Given an array of integers arr and two integers k and threshold.
7+
* Return the number of sub-arrays of size k and average greater than or equal to threshold.
8+
*
9+
* Example 1:
10+
* Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
11+
* Output: 3
12+
* Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
13+
*
14+
* Example 2:
15+
* Input: arr = [1,1,1,1,1], k = 1, threshold = 0
16+
* Output: 5
17+
*
18+
* Example 3:
19+
* Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
20+
* Output: 6
21+
* Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
22+
*
23+
* Example 4:
24+
* Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7
25+
* Output: 1
26+
*
27+
* Example 5:
28+
* Input: arr = [4,4,4,4], k = 4, threshold = 1
29+
* Output: 1
30+
*
31+
* Constraints:
32+
* 1 <= arr.length <= 10^5
33+
* 1 <= arr[i] <= 10^4
34+
* 1 <= k <= arr.length
35+
* 0 <= threshold <= 10^4
36+
* */
37+
public class _1343 {
38+
public static class Solution1 {
39+
public int numOfSubarrays(int[] arr, int k, int threshold) {
40+
int sum = 0;
41+
for (int i = 0; i < k - 1; i++) {
42+
sum += arr[i];
43+
}
44+
int count = 0;
45+
for (int i = k - 1; i < arr.length; i++) {
46+
sum += arr[i];
47+
if (i - k >= 0) {
48+
sum -= arr[i - k];
49+
}
50+
if (sum / k >= threshold) {
51+
count++;
52+
}
53+
}
54+
return count;
55+
}
56+
}
57+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1343;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1343Test {
10+
private static _1343.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1343.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.numOfSubarrays(new int[]{2, 2, 2, 2, 5, 5, 5, 8}, 3, 4));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)