Skip to content

Commit 9a9d0ae

Browse files
committed
added solution to subarray product less than k
1 parent f90adca commit 9a9d0ae

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Subarray product less than k
2+
3+
| # | Difficulty | Tag(s) | Link |
4+
| --- | ---------- | -------------- | --------------------------------------------------------------------------- |
5+
| 21 | Medium | Sliding window | [View problem](https://leetcode.com/problems/subarray-product-less-than-k/) |
6+
7+
## Approaches
8+
9+
- Brute force
10+
- Sliding window
11+
12+
### Brute force
13+
14+
- starting at each number of the array, analyze each subarray ending at the last number of the array
15+
- calculate the product for each subarray and check if it is less than k
16+
- if the product for a subarray is not less than k, then we can break out of the inner loop and move on to the next number in the array
17+
- O(n^2) time complexity
18+
- O(1) space complexity
19+
20+
### Sliding window
21+
22+
- we have to use a dynamically-resizing window since no fixed size is given for a valid subarray
23+
- keep expanding the window until the product of the numbers in the window becomes greater than or equal to k
24+
- then keep shrinking the window until the product becomes less than k again
25+
- if k is 0, then there won't be any valid subarray
26+
- O(n) time complexity
27+
- O(1) space complexity
28+
29+
#### How many valid subarrays are there in a window?
30+
31+
To calculate the number of valid subarrays in a window, it is important to realize two things:
32+
33+
- if the product of numbers in a window is less than k, then the product of numbers in all shorter subarrays ending with the last number in the window will also be less than k
34+
- the total number of valid subarrays in a window is `endIndex - startIndex + 1`
35+
- `endIndex - startIndex` gives the number of subarrays other than the subarray formed by the end number itself
36+
- 1 is added to account for the subarray formed by the end number
37+
- so the number of subarrays is equal to the size of the window
38+
39+
```js
40+
/*
41+
- let's consider this array -> [1, 2, 3, 4], and k = 15
42+
- window [1], startIndex = 0, endIndex = 0
43+
- the only valid subarray is formed by the number itself, [1]
44+
- count = 0 - 0 + 1 = 1
45+
- window [1, 2], startIndex = 0, endIndex = 1
46+
- there are 2 valid subarrays ending with 2 - [2] and [1, 2]
47+
- count = 1 - 0 + 1 = 2
48+
- window [1, 2, 3], startIndex = 0, endIndex = 2
49+
- there are 3 valid subarrays ending with 3 - [3], [2, 3], [1, 2, 3]
50+
- count = 2 - 0 + 1 = 3
51+
- window [1, 2, 3, 4], startIndex = 0, endIndex = 3
52+
- product of this window is 24 which is not less than 15
53+
- so we have to shrink the window
54+
- window [2, 3, 4], startIndex = 1, endIndex = 3
55+
- product is still not less than 15, so shrink the window again
56+
- window [3, 4], startIndex = 2, endIndex = 3
57+
- there are 2 valid subarrays ending with 4 - [4] and [3, 4]
58+
- count = 3 - 2 + 1 = 2
59+
- total number of valid subarrays = 1 + 2 + 3 + 2 = 8
60+
*/
61+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Sliding window
2+
function numSubarrayProductLessThanK(nums, k) {
3+
if (k <= 1) return 0;
4+
if (nums.length === 1) return nums[0] < k ? 1 : 0;
5+
6+
let start = 0;
7+
let count = 0;
8+
let currentProduct = 1;
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
const num = nums[i];
12+
currentProduct = currentProduct * num;
13+
14+
while (currentProduct >= k) {
15+
// shrink the window
16+
currentProduct = currentProduct / nums[start];
17+
start++;
18+
}
19+
20+
count += i - start + 1;
21+
}
22+
23+
return count;
24+
}
25+
26+
console.log(numSubarrayProductLessThanK([10, 5, 2, 6], 100));
27+
console.log(numSubarrayProductLessThanK([4, 12, 6, 7, 60], 60));
28+
console.log(numSubarrayProductLessThanK([4, 12, 60, 7, 6], 60));
29+
console.log(numSubarrayProductLessThanKV2([10, 2, 2, 5, 4, 4, 4, 3, 7, 7], 289));
30+
console.log(numSubarrayProductLessThanK([1, 2, 3], 0));
31+
console.log(numSubarrayProductLessThanK([1, 1, 1], 1));
32+
console.log(numSubarrayProductLessThanK([5], 10));
33+
console.log(numSubarrayProductLessThanK([4], 2));
34+
console.log(numSubarrayProductLessThanK([4, 2], 2));
35+
36+
// Brute force
37+
function numSubarrayProductLessThanKV2(nums, k) {
38+
if (k === 0) return 0;
39+
if (nums.length === 1) return nums[0] < k ? 1 : 0;
40+
41+
let count = 0;
42+
43+
for (let i = 0; i < nums.length; i++) {
44+
let product = 1;
45+
46+
for (let j = i; j < nums.length; j++) {
47+
product = product * nums[j];
48+
if (product < k) count++;
49+
else break;
50+
}
51+
}
52+
53+
return count;
54+
}
55+
56+
console.log('---------');
57+
console.log(numSubarrayProductLessThanKV2([10, 5, 2, 6], 100));
58+
console.log(numSubarrayProductLessThanKV2([4, 12, 6, 7, 60], 60));
59+
console.log(numSubarrayProductLessThanKV2([4, 12, 60, 7, 6], 60));
60+
console.log(numSubarrayProductLessThanKV2([10, 2, 2, 5, 4, 4, 4, 3, 7, 7], 289));
61+
console.log(numSubarrayProductLessThanKV2([1, 2, 3], 0));
62+
console.log(numSubarrayProductLessThanKV2([5], 10));
63+
console.log(numSubarrayProductLessThanKV2([4], 2));
64+
console.log(numSubarrayProductLessThanKV2([4, 2], 2));

0 commit comments

Comments
 (0)