-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (28 loc) · 922 Bytes
/
Main.java
File metadata and controls
35 lines (28 loc) · 922 Bytes
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
33
34
35
// LeetCode Problem: https://leetcode.com/problems/minimum-size-subarray-sum/
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int sum = 0, i = 0, j = 0;
int minLength = Integer.MAX_VALUE;
while(j < nums.length){
sum += nums[j];
while (sum >= target) {
minLength = Math.min(minLength, j - i + 1);
sum -= nums[i];
i++;
}
j++;
}
if(minLength == Integer.MAX_VALUE)
minLength = 0;
return minLength;
}
}
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {2,3,1,2,4,3};
int target = 7;
int result = solution.minSubArrayLen(target, nums);
System.out.println("Minimum size subarray length: " + result);
}
}