-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinSizeSubarray.java
More file actions
32 lines (25 loc) · 1.19 KB
/
MinSizeSubarray.java
File metadata and controls
32 lines (25 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int minLength = n + 1; // Initialize to a value greater than any possible length
int currentSum = 0;
int start = 0;
for (int end = 0; end < n; end++) {
currentSum += nums[end]; // Expand the window by adding nums[end]
// Shrink from the left as long as the condition is met
while (currentSum >= target) {
minLength = Math.min(minLength, end - start + 1); // Update minimum length
currentSum -= nums[start]; // Remove nums[start] from sum
start++; // Move start forward
}
}
return minLength == n + 1 ? 0 : minLength; // Return result
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test cases
System.out.println(solution.minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3})); // Output: 2
System.out.println(solution.minSubArrayLen(4, new int[]{1, 4, 4})); // Output: 1
System.out.println(solution.minSubArrayLen(11, new int[]{1, 1, 1, 1, 1, 1, 1, 1})); // Output: 0
}
}