Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a3e950c

Browse files
committed
Add solution #2302
1 parent 5937b7c commit a3e950c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,505 LeetCode solutions in JavaScript
1+
# 1,506 LeetCode solutions in JavaScript
22

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

@@ -1395,6 +1395,7 @@
13951395
2244|[Minimum Rounds to Complete All Tasks](./solutions/2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
13961396
2270|[Number of Ways to Split Array](./solutions/2270-number-of-ways-to-split-array.js)|Medium|
13971397
2300|[Successful Pairs of Spells and Potions](./solutions/2300-successful-pairs-of-spells-and-potions.js)|Medium|
1398+
2302|[Count Subarrays With Score Less Than K](./solutions/2302-count-subarrays-with-score-less-than-k.js)|Hard|
13981399
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
13991400
2338|[Count the Number of Ideal Arrays](./solutions/2338-count-the-number-of-ideal-arrays.js)|Hard|
14001401
2342|[Max Sum of a Pair With Equal Sum of Digits](./solutions/2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 2302. Count Subarrays With Score Less Than K
3+
* https://leetcode.com/problems/count-subarrays-with-score-less-than-k/
4+
* Difficulty: Hard
5+
*
6+
* The score of an array is defined as the product of its sum and its length.
7+
* - For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5) * 5 = 75.
8+
*
9+
* Given a positive integer array nums and an integer k, return the number of non-empty subarrays
10+
* of nums whose score is strictly less than k.
11+
*
12+
* A subarray is a contiguous sequence of elements within an array.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
var countSubarrays = function(nums, k) {
21+
let result = 0;
22+
let currentSum = 0;
23+
let left = 0;
24+
25+
for (let right = 0; right < nums.length; right++) {
26+
currentSum += nums[right];
27+
while (currentSum * (right - left + 1) >= k) {
28+
currentSum -= nums[left];
29+
left++;
30+
}
31+
result += right - left + 1;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)