Skip to content

Commit 49d3ace

Browse files
committed
Add solution #1343
1 parent 5fe7c88 commit 49d3ace

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,242 LeetCode solutions in JavaScript
1+
# 1,243 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1019,6 +1019,7 @@
10191019
1339|[Maximum Product of Splitted Binary Tree](./solutions/1339-maximum-product-of-splitted-binary-tree.js)|Medium|
10201020
1340|[Jump Game V](./solutions/1340-jump-game-v.js)|Hard|
10211021
1342|[Number of Steps to Reduce a Number to Zero](./solutions/1342-number-of-steps-to-reduce-a-number-to-zero.js)|Easy|
1022+
1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](./solutions/1343-number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold.js)|Medium|
10221023
1351|[Count Negative Numbers in a Sorted Matrix](./solutions/1351-count-negative-numbers-in-a-sorted-matrix.js)|Easy|
10231024
1352|[Product of the Last K Numbers](./solutions/1352-product-of-the-last-k-numbers.js)|Medium|
10241025
1356|[Sort Integers by The Number of 1 Bits](./solutions/1356-sort-integers-by-the-number-of-1-bits.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
3+
* https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers arr and two integers k and threshold, return the number of sub-arrays
7+
* of size k and average greater than or equal to threshold.
8+
*/
9+
10+
/**
11+
* @param {number[]} arr
12+
* @param {number} k
13+
* @param {number} threshold
14+
* @return {number}
15+
*/
16+
var numOfSubarrays = function(arr, k, threshold) {
17+
let result = 0;
18+
let windowSum = 0;
19+
const minSum = k * threshold;
20+
21+
for (let i = 0; i < k; i++) {
22+
windowSum += arr[i];
23+
}
24+
25+
if (windowSum >= minSum) result++;
26+
27+
for (let i = k; i < arr.length; i++) {
28+
windowSum += arr[i] - arr[i - k];
29+
if (windowSum >= minSum) result++;
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)