c++ O(n) solution


  • 0
    D
    class Solution {
    public:
        int minSubArrayLen(int s, vector<int>& nums) {
            int sum = 0, min_size = nums.size() + 1, j = 0;
            for(int i = 0; i < nums.size(); i++) {
                while(j < nums.size() && sum < s) {
                    sum += nums[j++];
                }
                min_size = sum >= s ? min(min_size, j - i) : min_size;
                sum -= nums[i];
            }
            return min_size > nums.size() ? 0 : min_size;
        }
    };
    

  • 0
    H

    hello dr.pro
    I don't think this is a O(n) solution, can you explain it to me?
    thanks very much~


Log in to reply
 

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