Leetcode 713. Subarray Product Less Than K

文章作者:Tyan
博客:noahsnail.com  |  CSDN  | 

1. Description

Leetcode 713. Subarray Product Less Than K_第1张图片
Subarray Product Less Than K

2. Solution

class Solution {
public:
    int numSubarrayProductLessThanK(vector& 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;
    }
};

Reference

  1. https://leetcode.com/problems/subarray-product-less-than-k/description/

你可能感兴趣的:(Leetcode 713. Subarray Product Less Than K)