O(n) 8ms C++ method


  • 1
    1
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int start = 0, end = 0, n = nums.size(), min_len = 0, sum = 0;
            while (end < n) {
                while (sum < s && end < n) {
                    sum += nums[end++];
                }
                while (sum >= s && start < end) {
                    if (min_len == 0 || min_len > end - start)
                        min_len = end - start;
                    sum -= nums[start++];
                }
            }
            return min_len;
        }
    };

Log in to reply
 

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