<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>

from typing import List

class Solution:
    def minSizeSubarray(self, nums: List[int], target: int) -> int:
        n = len(nums)
        total_sum = sum(nums)
        
        # If the total sum of nums is greater than or equal to target,
        # we might be able to find the subarray in a single pass.
        # Otherwise, we might need multiple cycles of nums.
        
        # We need to consider target % total_sum to handle multiple cycles.
        # Let's handle the prefix sum with modulo.
        
        prefix_sum = 0
        min_length = float('inf')
        prefix_map = {0: -1}  # To handle subarray starting from index 0
        
        # First pass for dealing with one cycle and also preparing for more cycles
        for i in range(2 * n):  # We iterate twice the length for safety
            prefix_sum += nums[i % n]
            if prefix_sum == target:
                min_length = min(min_length, i + 1)
            
            # Calculate the sum needed to reach the target
            needed = prefix_sum - target
            
            if needed in prefix_map:
                min_length = min(min_length, i - prefix_map[needed])
            
            # Store the first occurrence of this prefix sum
            if prefix_sum not in prefix_map:
                prefix_map[prefix_sum] = i
        
        # If target is greater than total_sum, we need multiple cycles
        if target > total_sum:
            # Calculate how many full cycles we need
            full_cycles = target // total_sum
            remainder_target = target % total_sum
            
            # If the remainder target is zero, we just need full_cycles * n
            if remainder_target == 0:
                return full_cycles * n
            
            # Otherwise, we need to find the remainder in the prefix sums
            prefix_sum = 0
            prefix_map = {0: -1}  # Resetting for new calculation
            
            for i in range(2 * n):  # Again iterate twice for safety
                prefix_sum += nums[i % n]
                if prefix_sum == remainder_target:
                    min_length = min(min_length, i + 1)
                
                needed = prefix_sum - remainder_target
                
                if needed in prefix_map:
                    min_length = min(min_length, i - prefix_map[needed])
                
                if prefix_sum not in prefix_map:
                    prefix_map[prefix_sum] = i
            
            if min_length == float('inf'):
                return -1
            
            return min_length + full_cycles * n
        
        return min_length if min_length != float('inf') else -1
469