From f4be7c8d7a797f7fefb44aa3c1a97bc9a5010a1f Mon Sep 17 00:00:00 2001 From: rickben Date: Sat, 31 Oct 2020 17:05:52 +0200 Subject: [PATCH] added solution in Java for Sub array product - problem 713 --- .../src/net/kenyang/algorithm/SubArrayProduct | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Java/src/net/kenyang/algorithm/SubArrayProduct diff --git a/Java/src/net/kenyang/algorithm/SubArrayProduct b/Java/src/net/kenyang/algorithm/SubArrayProduct new file mode 100644 index 0000000000..f0bb90bb77 --- /dev/null +++ b/Java/src/net/kenyang/algorithm/SubArrayProduct @@ -0,0 +1,19 @@ +public class SubArrayProduct { + public int numSubarrayProductLessThanK(int[] nums, int k) { + int counter_less_than_k = 0; + int product = 1; + int head = 0, tail = 0; + + while (head < nums.length && tail <= head) { + product *= nums[head]; + + while (product >= k && tail <= head) { + product = product / nums[tail]; + tail++; + } + counter_less_than_k += head - tail + 1; + head++; + } + return counter_less_than_k; + } +} \ No newline at end of file