Java O(N) Solution


  • 0
    H
    class Solution {
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            int ans = 0;
            int combo = 0;
            int prod = 1;
            for (int i = 0; i < nums.length; ++i) {
                ++combo;
                prod *= nums[i];
                while (prod >= k && combo > 0) {
                    prod /= nums[i - --combo];
                }
                ans += combo;
            }
            return ans;
        }
    }
    

Log in to reply
 

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