File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 28
28
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
29
29
648|[ Replace Words] ( ./0648-replace-words.js ) |Medium|
30
30
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|
31
32
722|[ Remove Comments] ( ./0722-remove-comments.js ) |Medium|
32
33
739|[ Daily Temperatures] ( ./0739-daily-temperatures.js ) |Medium|
33
34
791|[ Custom Sort String] ( ./0791-custom-sort-string.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments