Skip to content

Commit 03d7058

Browse files
update 713
1 parent c65ee9c commit 03d7058

File tree

2 files changed

+13
-9
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+13
-9
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_713.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
public class _713 {
44
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+
*/
59
public int numSubarrayProductLessThanK(int[] nums, int k) {
610
if (k < 2) {
711
return 0;
812
}
913
int result = 0;
1014
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++) {
1216
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++];
1519
}
16-
result += right - i + 1;
20+
result += right - left + 1;
1721
}
1822
return result;
1923
}

Diff for: src/test/java/com/fishercoder/firstthousand/_713Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.fishercoder.firstthousand;
22

33
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;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _713Test {
1010
private static _713.Solution1 solution1;
1111
private static int[] nums;
1212
private static int k;
1313

14-
@BeforeClass
15-
public static void setup() {
14+
@BeforeEach
15+
public void setup() {
1616
solution1 = new _713.Solution1();
1717
}
1818

0 commit comments

Comments
 (0)