# 713.Subarray Product Less Than K

**713.Subarray Product Less Than K**

难度:Medium 给定一个正整数数组 nums。

找出该数组内乘积小于 k 的连续的子数组的个数。

```
示例 1:

输入: nums = [10,5,2,6], k = 100
输出: 8
解释: 8个乘积小于100的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
需要注意的是 [10,5,2] 并不是乘积小于100的子数组。
说明:

0 < nums.length <= 50000
0 < nums[i] < 1000
0 <= k < 10^6
```

Method: 因为是连续子数组，可以以左右指针来计数。左边起始点为left，每向右增加一个，实际上增加的连续子数组的数目是(right-left+1)，因为增加的连续子数组数目必须包含right，否则就重复了。 然后从左往右遍历，如果乘积超过K了，则left右移，直到小于k为止。

```
class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        if(k<=1 || nums.empty()) return 0;
        if(nums.size()==1) return nums[0]<k;
        int prod=1;
        int left = 0;
        int res=0;
      for(int i=0;i<nums.size();i++)
        {
            prod*=nums[i];
            while(prod>=k)
                {
                prod = prod/nums[left++];
            }
            res+=i-left+1;
        }
        return res;

    }
};
```

> 执行用时 :264 ms, 在所有 C++ 提交中击败了97.70%的用户\
> 内存消耗 :83.7 MB, 在所有 C++ 提交中击败了8.27%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/713.subarray_product_less_than_k.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
