<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:
        # https://algo.monster/liteproblems/2875
        total_sum = sum(nums)  # Calculate the total sum of all numbers in the array
        num_count = len(nums)  # Get the length of the array

        # Initialize an 'a' which is the number of complete array rotations required
        num_full_rotations = 0
        if target > total_sum:
            num_full_rotations = (target // total_sum) * num_count
            target -= (target // total_sum) * total_sum  # Update the target after the full rotations

        # If target equals total_sum, return the array length
        if target == total_sum:
            return num_count

        pos = {0: -1}  # Create a dictionary to store the prefix sum indices
        prefix_sum = 0  # Initialize the prefix sum
        min_length = inf  # Set initial min length to infinity

        # Traverse through the array
        for i, num in enumerate(nums):
            prefix_sum += num  # Update prefix sum

            # Check if there's a subarray that ends at index i with sum equals target
            if (t := prefix_sum - target) in pos:
                min_length = min(min_length, i - pos[t])

            # Check if there is a circular subarray that sums to target
            if (t := prefix_sum - (total_sum - target)) in pos:
                min_length = min(min_length, num_count - (i - pos[t]))

            # Store the latest index of this prefix sum
            pos[prefix_sum] = i

        # If min_length is still infinity, no subarray was found. Return -1.
        # Otherwise, return the answer which includes the rotations 'a' + the found subarray length 'b'
        return -1 if min_length == inf else num_full_rotations + min_length

        # total = sum(nums)
        # nums = nums * max(2, (target // total + 10))
        # i, n = -1, len(nums)
        # cur, ans = 0, float('inf')
        # hm = {}
        # for i in range(n):
        #     cur += nums[i]
        #     if cur - target in hm:
        #         ans = min(ans, i - hm[cur - target])
        #     hm[cur] = i
        # return ans if ans != float('inf') else -1

        # print(nums)
        # while i < n:
        #     j = i + 1
        #     while j < n and cur < target:
        #         cur += nums[j]
        #         j += 1
        #     if cur == target:
        #         ans = min(ans, j - i + 1)    
        #         i += 1        
        #     else:
        #         k = i + 1
        #         while k < n and cur > target:
        #             cur -= nums[k]
        #             k += 1
        #         if cur == target:
        #             ans = min(ans, j - k + 1)
        #         i = k
        # return ans if ans != float('inf') else -1

            
403