<p>You are given a <strong>0-indexed</strong> array <code>nums</code> and an integer <code>target</code>.</p>

<p>A <strong>0-indexed</strong> array <code>infinite_nums</code> is generated by infinitely appending the elements of <code>nums</code> to itself.</p>

<p>Return <em>the length of the <strong>shortest</strong> subarray of the array </em><code>infinite_nums</code><em> with a sum equal to </em><code>target</code><em>.</em> If there is no such subarray return <code>-1</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3], target = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example infinite_nums = [1,2,3,1,2,3,1,2,...].
The subarray in the range [1,2], has the sum equal to target = 5 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,1,2,3], target = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].
The subarray in the range [4,5], has the sum equal to target = 4 and length = 2.
It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,4,6,8], target = 3
<strong>Output:</strong> -1
<strong>Explanation:</strong> In this example infinite_nums = [2,4,6,8,2,4,6,8,...].
It can be proven that there is no subarray with sum equal to target = 3.
</pre>

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

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

class Solution:
    def minSizeSubarray(self, nums: List[int], target: int) -> int:
        N = len(nums)

        # convert big number to smaller one
        sum_nums = sum(nums)
        layer_deleted = target // sum_nums
        target = target - layer_deleted * sum_nums
        
        def get_size(i, j):
            real_i = i[1] + (0 if i[0] == 0 else N)
            real_j = j[1] + (0 if j[0] == 0 else N)
            return real_j - real_i + 1
        def i_bigger_j(i, j):
            if i[0] < j[0]:
                return False
            return i[1] > j[1]
        def move(i):
            i[1] += 1
            if i[1] == N:
                i[0] += 1
                i[1] = 0

        # sliding window
        smallest_size = None
        # (layer (0 or 1), index in that layer)
        i, j = [0, 0], [0, 0]
        cur_sum = 0
        while j[0] <= 1 and j[1] < N:
            # update the result
            cur_sum += nums[j[1]]

            # Increase i
            while cur_sum >= target and not i_bigger_j(i, j):
                # print(i, j, cur_sum)
                if cur_sum == target:
                    # update result
                    if smallest_size == None:
                        smallest_size = get_size(i, j)
                    else:
                        smallest_size = min(smallest_size, get_size(i, j))

                cur_sum -= nums[i[1]]
                move(i)
            
            # Increase j at end of loop
            move(j)
        
        if smallest_size == None:
            return -1
        else:
            return smallest_size + layer_deleted * N
485