Subarray Product Less Than K

时间:2022-06-22
本文章向大家介绍Subarray Product Less Than K,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. Description

2. Solution

class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        if(k <= 1) {
            return 0;
        } 
        int i = 0;
        int j = 0;
        int count = 0;
        int product = 1;
        while(j < nums.size()) {
            product *= nums[j];
            while(product >= k) {
                product /= nums[i];
                i++;
            }
            count += j - i + 1;
            j++;
        }
        return count;
    }
};