C# Solution


  • 1
    J

    C# solution

    Iterate over the array and add the length of the maximum subarray for each location.

    For e.g.input array nums = [10, 5, 2, 6] and k = 100

    lengths at each location are [1, 2, 2, 3]
    return sum of all lengths, that is 1 + 2 + 2 + 3 = 8;

    For corner cases like k = 0 and k = 1 return meaningful values.

    public int NumSubarrayProductLessThanK(int[] nums, int k) {
                if (nums == null || k == 0 || k == 1)
                {
                    return 0;
                }
    
                int p = 1;
                int i = 0;
                int len = 0;
                int count = 0;
    
                while (i < nums.Length)
                {
                    p *= nums[i];
    
                    if (p < k)
                    {
                        count += ++len;
                    }
                    else
                    {
                        while (p >= k)
                        {
                            p /= nums[i - len];
                            len--;
                        }
    
                        count += ++len;
                    }
    
                    i++;
                }
    
                return count;
        }
    

  • 1
    C

    @jainchethan87 said in C# Solution:

    public int NumSubarrayProductLessThanK(int[] nums, int k) {
    if (nums == null || k == 0 || k == 1)
    {
    return 0;
    }

            int p = 1;
            int i = 0;
            int len = 0;
            int count = 0;
    
            while (i < nums.Length)
            {
                p *= nums[i];
    
                if (p < k)
                {
                    count += ++len;
                }
                else
                {
                    while (p >= k)
                    {
                        p /= nums[i - len];
                        len--;
                    }
    
                    count += ++len;
                }
    
                i++;
            }
    
            return count;
    }
    

    Thanks, but isn't it better to avoid duplicate code by removing else? Here's the edited version of your code:

    public int NumSubarrayProductLessThanK(int[] nums, int k) {
                if (nums == null || k == 0 || k == 1)
                {
                    return 0;
                }
    
                int p = 1;
                int i = 0;
                int len = 0;
                int count = 0;
                while (i < nums.Length)
                {
                    p *= nums[i];
                    if (p >= k)
                    {
                        while (p >= k)
                        {
                            p /= nums[i - len];
                            len--;
                        }
                    }
                    count += ++len;
                    i++;
                }
                return count;
        }
    

  • 0
    J

Log in to reply
 

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