<p>Given an array of positive integers <code>nums</code> and a positive integer <code>target</code>, return <em>the <strong>minimal length</strong> of a </em><span data-keyword="subarray-nonempty"><em>subarray</em></span><em> whose sum is greater than or equal to</em> <code>target</code>. If there is no such subarray, return <code>0</code> instead.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> target = 7, nums = [2,3,1,2,4,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The subarray [4,3] has the minimal length under the problem constraint.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> target = 4, nums = [1,4,4]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> target = 11, nums = [1,1,1,1,1,1,1,1]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> If you have figured out the <code>O(n)</code> solution, try coding another solution of which the time complexity is <code>O(n log(n))</code>.
/* Your implementation of `Solution` goes here. */
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int len = nums.size();
        int j = 0;
        int sum = 0, min = len + 1;
        for(int i = 0; i < len; i++) {
            sum += nums[i];
            if(sum < target) continue;
            while(sum >= target && j <= i) {
                min = (i-j+1 < min) ? i-j+1 : min;
                sum -= nums[j];
                j++;
            }
        }
        if(min > len) return 0;
        return min;
    }
};
/* Uncomment the following code snippet on Leetcode for performance optimization. */
int init = [] {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ofstream out("user.out");
    cout.rdbuf(out.rdbuf());

    // for (string s; getline(cin, s); cout << '\n') {
    //     string t;
    //     getline(cin, t);
    //     int tar = stoi(t);

    //     for (int i = 0, _i = 1, _n = s.length(); _i < _n; ++i, ++_i) {
    //         bool _neg = 0;
    //         if (s[_i] == '-')
    //             ++_i, _neg = 1;
    //         int v = s[_i++] & 15;
    //         while ((s[_i] & 15) < 10)
    //             v = v * 10 + (s[_i++] & 15);
    //         if (_neg)
    //             v = -v;
    //         if (v == tar) {
    //             cout << i;
    //             goto next;
    //         }
    //         if (v > tar)
    //             break;
    //     }
    //     cout << -1;
    // next:;
    // }
    // exit(0);
    return 0;
}();
30900