Simple Java Solution, Two Pointers


  • 0
    O
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            if (k<=1) return 0; 
            int i=0, j=0, prod=1, count=0; 
            while (j<nums.length){
                prod *=nums[j++]; 
                while (prod>=k){
                    prod/=nums[i++]; 
                }
                count+=(j-i); 
            }
            return count; 
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.