Subarray Product Less Than K

https://www.lintcode.com/problem/subarray-product-less-than-k/description

public class Solution {
    /**
     * @param nums: an array
     * @param k: an integer
     * @return: the number of subarrays where the product of all the elements in the subarray is
     * less than k
     */
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        // Write your code here
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            if (num < k) {
                result++;
                for (int j = i + 1; j < nums.length; j++) {
                    num *= nums[j];
                    if (num < k) {
                        result++;
                    } else {
                        break;
                    }
                }
            }
        }
        return result;
    }
}

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