Honestly, the first approach I tried is DFS, which leads to TLE.

Then I came up with this sliding window solution. See the solution below:

- Expand the window as large as possible;
- Compute the number of valid subarray.

```
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
int p = 0;
int product = 1;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
if (p < i) p = i;
while (p < nums.length && nums[p] * product < k) {
product *= nums[p];
p++;
}
sum += p - i;
if (p != i) product /= nums[i];
}
return sum;
}
}
```