Skip to content

Commit 95f3439

Browse files
committed
Add solution #713
1 parent 9980cf4 commit 95f3439

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
2929
648|[Replace Words](./0648-replace-words.js)|Medium|
3030
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
31+
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
3132
722|[Remove Comments](./0722-remove-comments.js)|Medium|
3233
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
3334
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 713. Subarray Product Less Than K
3+
* https://leetcode.com/problems/subarray-product-less-than-k/
4+
* Difficulty: Medium
5+
*
6+
* Your are given an array of positive integers nums.
7+
*
8+
* Count and print the number of (contiguous) subarrays where the
9+
* product of all the elements in the subarray is less than k.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var numSubarrayProductLessThanK = function(nums, k) {
18+
let result = 0;
19+
for (let right = 0, left = 0, product = 1; right < nums.length; right++) {
20+
product *= nums[right];
21+
while (product >= k && left < nums.length) {
22+
product /= nums[left];
23+
left++;
24+
}
25+
if (left < nums.length) {
26+
result = result + right - left + 1;
27+
}
28+
}
29+
return result;
30+
};

0 commit comments

Comments
 (0)