Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 458 Bytes

713.subarray-product-less-than-k.md

File metadata and controls

21 lines (18 loc) · 458 Bytes

双指针算法

固定右端点,求左端点的起始位置到右端点的长度,相加

代码实现

class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        int l = 0, r = 0, res = 0, p = 1;
        while (r < nums.length) {
            p *= nums[r++];
            while (p >= k && l < r){
                p /= nums[l++];
            }
            res += r - l;
        }
        return res;
    }
}