You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2800-2899/2875.Minimum Size Subarray in Infinite Array/README_EN.md
+164-2
Original file line number
Diff line number
Diff line change
@@ -51,16 +51,80 @@ It can be proven that there is no subarray with sum equal to target = 3.
51
51
52
52
## Solutions
53
53
54
+
**Solution 1: Prefix Sum + Hash Table**
55
+
56
+
First, we calculate the sum of all elements in the array $nums$, denoted as $s$.
57
+
58
+
If $target \gt s$, we can reduce $target$ to the range $[0, s)$ by subtracting $\lfloor \frac{target}{s} \rfloor \times s$ from it. Then, the length of the subarray is $a = \lfloor \frac{target}{s} \rfloor \times n$, where $n$ is the length of the array $nums$.
59
+
60
+
Next, we need to find the shortest subarray in $nums$ whose sum equals $target$, or the shortest subarray whose prefix sum plus suffix sum equals $s - target$. We can use prefix sum and a hash table to find such subarrays.
61
+
62
+
If we find such a subarray, the final answer is $a + b$. Otherwise, the answer is $-1$.
63
+
64
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where n is the length of the array $nums$.
1 commit comments
vercel[bot] commentedon Oct 3, 2023
Successfully deployed to the following URLs:
leetcode – ./
leetcode-doocs.vercel.app
leetcode-git-main-doocs.vercel.app
doocs-leetcode.vercel.app