Minimum Size Subarray Sum - Complete Solution Guide
Minimum Size Subarray Sum is LeetCode problem 209, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an array of positive integers nums and a positive integer target , return the minimal length of a subarray whose sum is greater than or equal to target . If there is no such subarray, return 0 instead. Example 1: Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the minimal length under the problem constraint. Example 2: Input: target = 4, nums = [1,4,4] Output: 1 Example 3: Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0 Constraints: 1 <= target
Detailed Explanation
The problem asks us to find the minimum length of a contiguous subarray within a given array of positive integers (`nums`) whose sum is greater than or equal to a given positive integer (`target`). If no such subarray exists, we should return 0.
Solution Approach
The solution uses a sliding window approach. We maintain a window defined by two pointers, `left` and `right`. We expand the window by incrementing the `right` pointer and adding the corresponding element to the `current_sum`. If the `current_sum` becomes greater than or equal to the `target`, we update the `min_len` with the current window size. Then, we shrink the window from the left by incrementing the `left` pointer and subtracting the corresponding element from the `current_sum`, continuing the process until `current_sum` is less than `target`. This process is repeated until the `right` pointer reaches the end of the array.
Step-by-Step Algorithm
- Step 1: Initialize `min_len` to `n + 1`, `left` to 0, and `current_sum` to 0. `min_len` is initialized to a value greater than the possible length of any subarray in `nums`, acting as a sentinel value.
- Step 2: Iterate through the array `nums` using the `right` pointer.
- Step 3: Add the element at the `right` index to the `current_sum`.
- Step 4: While the `current_sum` is greater than or equal to the `target`, update `min_len` to the minimum of its current value and the length of the current window (`right - left + 1`).
- Step 5: Subtract the element at the `left` index from the `current_sum` and increment the `left` pointer to shrink the window from the left.
- Step 6: Repeat steps 4 and 5 until `current_sum` is less than `target`.
- Step 7: After the loop finishes, if `min_len` is still equal to `n + 1`, it means no subarray was found that meets the criteria, so return 0. Otherwise, return `min_len`.
Key Insights
- Insight 1: The problem requires finding a *subarray*, meaning a contiguous sequence of elements within the original array.
- Insight 2: Since the numbers are positive, the sum of a subarray increases monotonically as we add more elements to the right. This allows us to use a sliding window approach.
- Insight 3: We need to efficiently update the sum of the current subarray and compare it with the target value to determine if the window needs to be shrunk from the left.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search, Sliding Window, Prefix Sum.
Companies
Asked at: Autodesk, DE Shaw, Darwinbox, DoorDash, Goldman Sachs, Nvidia, Oracle, Yandex.