<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>.
# answer = 0

# for r in range(nums):
#  l
# [2,3,1,2,4,3]
#  r
# sum = 2

#  l
# [2,3,1,2,4,3]
#    r  
# sum = 5

#  l
# [2,3,1,2,4,3]
#      r  
# sum = 6

#  l
# [2,3,1,2,4,3]
#        r  
# sum = 8

# while sum > target:
#     answer = r - l + 1
#     sum -= nums[l]
#     l += 1
    
#    l
# [2,3,1,2,4,3]
#        r  
# sum = 6  
    
#    l
# [2,3,1,2,4,3]
#          r  
# sum = 10 
    
#      l
# [2,3,1,2,4,3]
#          r  
# sum = 7
# answer = 4 - 2 + 1 = 3
    
#        l
# [2,3,1,2,4,3]
#          r  
# sum = 6
    
#        l
# [2,3,1,2,4,3]
#            r  
# sum = 9
# answer = 5 - 3 + 1 = 3

#          l
# [2,3,1,2,4,3]
#            r  
# sum = 7
# answer = 2


class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        answer = float('inf')
        window_sum = 0
        l = 0
        
        for r, current in enumerate(nums):
            window_sum += current
            
            # print('l', l)
            # print('r', r)
            # print('current', current)
            # print('sum', window_sum)
            
            while window_sum >= target:
                answer = min(answer, r - l + 1)
                # print('answer', answer)
                window_sum -= nums[l]
                l += 1
                
            print('-')
        
        if answer == float('inf'):
            return 0
        
        return answer
        
29500