We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b62e3a8 commit 9c70403Copy full SHA for 9c70403
Medium/Subarray Product Less Than K.java
@@ -1,15 +1,18 @@
1
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;
+ public int numSubarrayProductLessThanK(int[] nums, int k) {
+ if (k <= 1) {
+ return 0;
14
}
+ int prod = 1;
+ int ans = 0;
+ int left = 0;
+ for (int right = 0; right < nums.length; right++) {
+ prod *= nums[right];
+ while (prod >= k) {
+ prod /= nums[left++];
+ }
+ ans += right - left + 1;
15
16
+ return ans;
17
18
0 commit comments