File tree 2 files changed +13
-9
lines changed
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand
2 files changed +13
-9
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class _713 {
4
4
public static class Solution1 {
5
+ /**
6
+ * Classic two pointer technique, in this problem:
7
+ * the right pointer keeps moving forward and the left pointer moves forward when the current product >= k
8
+ */
5
9
public int numSubarrayProductLessThanK (int [] nums , int k ) {
6
10
if (k < 2 ) {
7
11
return 0 ;
8
12
}
9
13
int result = 0 ;
10
14
int product = 1 ;
11
- for (int i = 0 , right = 0 ; right < nums .length ; right ++) {
15
+ for (int left = 0 , right = 0 ; right < nums .length ; right ++) {
12
16
product *= nums [right ];
13
- while (i < nums .length && product >= k ) {
14
- product /= nums [i ++];
17
+ while (left < nums .length && product >= k ) {
18
+ product /= nums [left ++];
15
19
}
16
- result += right - i + 1 ;
20
+ result += right - left + 1 ;
17
21
}
18
22
return result ;
19
23
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .firstthousand ;
2
2
3
3
import com .fishercoder .solutions .firstthousand ._713 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _713Test {
10
10
private static _713 .Solution1 solution1 ;
11
11
private static int [] nums ;
12
12
private static int k ;
13
13
14
- @ BeforeClass
15
- public static void setup () {
14
+ @ BeforeEach
15
+ public void setup () {
16
16
solution1 = new _713 .Solution1 ();
17
17
}
18
18
You can’t perform that action at this time.
0 commit comments