Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f9566f

Browse files
Refactor numSubarrayProductLessThanK method: simplify logic and remove commented-out code
1 parent 687cbcf commit 1f9566f

File tree

1 file changed

+58
-39
lines changed

1 file changed

+58
-39
lines changed

‎leetcode/713.subarray_product_less_than_k.dart

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,68 @@ void main() {
1111
}
1212

1313
class Solution {
14-
////! Done: Solved the problem
14+
////! Done: Solved the problem (MY SOLUTION)
1515
/// TC: O(n) SC: O(1)
16+
// int numSubarrayProductLessThanK(List<int> nums, int k) {
17+
// /// Check if empty Array
18+
// if (nums.isEmpty) {
19+
// return 0;
20+
// }
21+
22+
// /// Check if the list only one item
23+
// if (nums.length == 1) {
24+
// if (nums.first < k) {
25+
// return 1;
26+
// } else {
27+
// return 0;
28+
// }
29+
// }
30+
31+
// int subArraysCount = 0;
32+
// int startPointerIndex = 0;
33+
// int endPointerIndex = 0;
34+
// int score = nums[endPointerIndex];
35+
36+
// while (endPointerIndex < nums.length) {
37+
// if (score < k) {
38+
// subArraysCount += endPointerIndex - startPointerIndex + 1;
39+
// endPointerIndex++;
40+
// if (endPointerIndex < nums.length) {
41+
// score *= nums[endPointerIndex];
42+
// }
43+
// } else {
44+
// if (startPointerIndex < nums.length) {
45+
// score ~/= nums[startPointerIndex];
46+
// } else {
47+
// break;
48+
// }
49+
// startPointerIndex++;
50+
// if (endPointerIndex < startPointerIndex) {
51+
// endPointerIndex++;
52+
// if (endPointerIndex < nums.length) {
53+
// score *= nums[endPointerIndex];
54+
// }
55+
// }
56+
// }
57+
// }
58+
59+
// return subArraysCount;
60+
// }
61+
62+
/// Another Solution
63+
/// AI Solution and from the course
1664
int numSubarrayProductLessThanK(List<int> nums, int k) {
17-
/// Check if empty Array
18-
if (nums.isEmpty) {
19-
return 0;
20-
}
21-
22-
/// Check if the list only one item
23-
if (nums.length == 1) {
24-
if (nums.first < k) {
25-
return 1;
26-
} else {
27-
return 0;
28-
}
29-
}
30-
3165
int subArraysCount = 0;
32-
int startPointerIndex = 0;
33-
int endPointerIndex = 0;
34-
int score = nums[endPointerIndex];
35-
36-
while (endPointerIndex < nums.length) {
37-
if (score < k) {
38-
subArraysCount += endPointerIndex - startPointerIndex + 1;
39-
endPointerIndex++;
40-
if (endPointerIndex < nums.length) {
41-
score *= nums[endPointerIndex];
42-
}
43-
} else {
44-
if (startPointerIndex < nums.length) {
45-
score ~/= nums[startPointerIndex];
46-
} else {
47-
break;
48-
}
49-
startPointerIndex++;
50-
if (endPointerIndex < startPointerIndex) {
51-
endPointerIndex++;
52-
if (endPointerIndex < nums.length) {
53-
score *= nums[endPointerIndex];
54-
}
55-
}
66+
int left = 0;
67+
int product = 1;
68+
69+
for (int right = 0; right < nums.length; right++) {
70+
product *= nums[right];
71+
while (right >= left && product >= k) {
72+
product ~/= nums[left];
73+
left++;
5674
}
75+
subArraysCount += right - left + 1;
5776
}
5877

5978
return subArraysCount;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /