2 years ago

#68060

test-img

Prakhar Agrawal

why is it showing time limit exceeded?

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.

 class Solution{
    public:
int minSubArrayLen(int target, vector<int> &nums)
{
    int right = 0;
    int left = 0;
    int size = nums.size();
    long sum = 0;
    int length = 100000;
    while (left < size)
    {
        sum = sum + nums[right];
        if (sum >= target)
        {
            length = min(length, right - left + 1);

            sum = sum - nums[left];
            left++;
            if (right != size)
                right++;
        }

        else if (right != size)
            right++;
    }
    return length;
}
};

c++

size

minimum

sub-array

0 Answers

Your Answer

Accepted video resources