share my java solution!


  • 0
    T
    
    class Solution {
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            if(k==0){
                return 0;
            }
            
            int sum = 1;
            int start = 0;
            int count = 0;
            for(int i=0;i<nums.length;i++){
                sum*=nums[i];
                if(sum<k){
                    count+=i-start+1;
                }else{
                    while(start<=i&&sum>=k){
                        sum/=nums[start];
                        start++;
                    }
                    if(i>=start){
                        count+=i-start+1;
                    }
                }
            }
            
            return count;
        }
    }
    

Log in to reply
 

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