Skip to content

Commit 9c70403

Browse files
committed
Modified Subarray Product Less Than K.java
1 parent b62e3a8 commit 9c70403

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
class Solution {
2-
public int numSubarrayProductLessThanK(int[] nums, int k) {
3-
if (k<2) return 0;
4-
int result = 0;
5-
int product = 1;
6-
for (int i = 0, right = 0; right < nums.length; right++) {
7-
product *= nums[right];
8-
while (i < nums.length && product >= k) {
9-
product /= nums[i++];
10-
}
11-
result += right - i + 1;
12-
}
13-
return result;
2+
public int numSubarrayProductLessThanK(int[] nums, int k) {
3+
if (k <= 1) {
4+
return 0;
145
}
6+
int prod = 1;
7+
int ans = 0;
8+
int left = 0;
9+
for (int right = 0; right < nums.length; right++) {
10+
prod *= nums[right];
11+
while (prod >= k) {
12+
prod /= nums[left++];
13+
}
14+
ans += right - left + 1;
15+
}
16+
return ans;
17+
}
1518
}

0 commit comments

Comments
 (0)