Untitled

 avatar
unknown
plain_text
a month ago
738 B
1
Indexable
/********************************************************************** JAVA *****************************************************************/
// Approach: Standard Sliding Window Problem
// T.C: O(N)
// S.C: O(1)
public class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        if (k <= 1)
            return 0;

        int n = nums.length;
        int count = 0;

        int i = 0;
        int j = 0;
        int prod = 1;

        while (j < n) {
            prod *= nums[j];

            while (prod >= k) {
                prod /= nums[i];
                i++;
            }

            count += (j - i) + 1;
            j++;
        }

        return count;
    }
}
Editor is loading...
Leave a Comment