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 45caac1

Browse files
Add solution for Subarray Product Less Than K problem with comprehensive test cases
1 parent a9623a4 commit 45caac1

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Hash Table: https://leetcode.com/problems/find-all-duplicates-in-an-array/
4242
- https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/description/?envType=daily-question&envId=2025年05月10日
4343
- Bad Question : I don't recommend to solve: https://leetcode.com/problems/next-permutation/description/
44+
- https://leetcode.com/problems/subarray-product-less-than-k/description/
4445
-
4546

4647
### Hard Problems
@@ -59,7 +60,7 @@
5960
- Prefix product &&& Suffix product
6061
- Hash Table
6162
- Mark Indices --> By negative value
62-
-
63+
- Sliding Window
6364

6465

6566
### Algorithms
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// https://leetcode.com/problems/subarray-product-less-than-k/description/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
///
9+
stopwatch.stop();
10+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro s');
11+
}
12+
13+
class Solution {
14+
////! Done: Solved the problem
15+
int numSubarrayProductLessThanK(List<int> nums, int k) {
16+
/// Check if empty Array
17+
if (nums.isEmpty) {
18+
return 0;
19+
}
20+
21+
/// Check if the list only one item
22+
if (nums.length == 1) {
23+
if (nums.first < k) {
24+
return 1;
25+
} else {
26+
return 0;
27+
}
28+
}
29+
30+
int subArraysCount = 0;
31+
int startPointerIndex = 0;
32+
int endPointerIndex = 0;
33+
int score = nums[endPointerIndex];
34+
35+
while (endPointerIndex < nums.length) {
36+
if (score < k) {
37+
subArraysCount += endPointerIndex - startPointerIndex + 1;
38+
endPointerIndex++;
39+
if (endPointerIndex < nums.length) {
40+
score *= nums[endPointerIndex];
41+
}
42+
} else {
43+
if (startPointerIndex < nums.length) {
44+
score ~/= nums[startPointerIndex];
45+
} else {
46+
break;
47+
}
48+
startPointerIndex++;
49+
if (endPointerIndex < startPointerIndex) {
50+
endPointerIndex++;
51+
if (endPointerIndex < nums.length) {
52+
score *= nums[endPointerIndex];
53+
}
54+
}
55+
}
56+
}
57+
58+
return subArraysCount;
59+
}
60+
61+
int factorialIterative(int n) {
62+
int result = 1;
63+
for (int i = 1; i <= n; i++) {
64+
result *= i;
65+
}
66+
return result;
67+
}
68+
}
69+
70+
void runTests() {
71+
final Solution s = Solution();
72+
73+
group('Subarray Product Less Than K', () {
74+
// Basic examples from problem statement
75+
test('Example 1: [10,5,2,6], k=100 → 8', () {
76+
expect(s.numSubarrayProductLessThanK([10, 5, 2, 6], 100), equals(8));
77+
});
78+
79+
test('Example 2: [1,2,3], k=0 → 0', () {
80+
expect(s.numSubarrayProductLessThanK([1, 2, 3], 0), equals(0));
81+
});
82+
83+
// Edge cases
84+
test('Single element below k: [5], k=10 → 1', () {
85+
expect(s.numSubarrayProductLessThanK([5], 10), equals(1));
86+
});
87+
88+
test('Single element above k: [5], k=4 → 0', () {
89+
expect(s.numSubarrayProductLessThanK([5], 4), equals(0));
90+
});
91+
92+
test('Empty array: [], k=100 → 0', () {
93+
expect(s.numSubarrayProductLessThanK([], 100), equals(0));
94+
});
95+
96+
// All elements below k
97+
test('All elements below k: [1,2,3,4], k=100 → 10', () {
98+
expect(s.numSubarrayProductLessThanK([1, 2, 3, 4], 100), equals(10));
99+
});
100+
101+
// All elements above k
102+
test('All elements above k: [10,20,30], k=5 → 0', () {
103+
expect(s.numSubarrayProductLessThanK([10, 20, 30], 5), equals(0));
104+
});
105+
106+
test('With 1s: [1,1,1,1], k=2 → 10', () {
107+
expect(s.numSubarrayProductLessThanK([1, 1, 1, 1], 2), equals(10));
108+
});
109+
110+
// Large k
111+
test('Large k: [1,2,3], k=1000 → 6', () {
112+
expect(s.numSubarrayProductLessThanK([1, 2, 3], 1000), equals(6));
113+
});
114+
115+
// k=1 edge case
116+
test('k=1: [1,1,1], k=1 → 0', () {
117+
expect(s.numSubarrayProductLessThanK([1, 1, 1], 1), equals(0));
118+
});
119+
120+
// Large array
121+
test('Large array with small k', () {
122+
final nums = List.filled(30000, 2);
123+
expect(s.numSubarrayProductLessThanK(nums, 1), equals(0));
124+
});
125+
126+
test('Large array with large k', () {
127+
final nums = List.filled(30000, 1);
128+
expect(s.numSubarrayProductLessThanK(nums, 1000000),
129+
equals(30000 * 30001 ~/ 2));
130+
});
131+
132+
// Minimum constraints
133+
test('Minimum length: [1], k=1 → 0', () {
134+
expect(s.numSubarrayProductLessThanK([1], 1), equals(0));
135+
});
136+
137+
/// Leet Code
138+
test('Example: [1,2,3,4,5], k=1 → 0', () {
139+
expect(s.numSubarrayProductLessThanK([1, 2, 3, 4, 5], 1), equals(0));
140+
});
141+
});
142+
}

0 commit comments

Comments
(0)

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