-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSmallestSubArrWithGivenSum.java
33 lines (30 loc) · 1.05 KB
/
SmallestSubArrWithGivenSum.java
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
package algorithms.slidingwindows;
/**
* Smallest subarray with given sum
* https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1490s
* Related Topics : Sliding Window
*
* created by cenkc on 4/24/2020
*/
public class SmallestSubArrWithGivenSum {
public static void main (String[] args) {
int[] A = new int[]{4, 2, 1, 7, 8, 1 ,2, 8, 1, 0};
int K = 8;
SmallestSubArrWithGivenSum smallestSubArrWithGivenSum = new SmallestSubArrWithGivenSum();
System.out.println(smallestSubArrWithGivenSum.solution(A, K));
}
public int solution(int[] A, int K) {
int minWindowSize = Integer.MAX_VALUE;
int sumOfWindow = 0;
int windowStart = 0;
for (int windowEnd = 0; windowEnd < A.length; windowEnd++) {
sumOfWindow += A[windowEnd];
while (sumOfWindow >= K) {
minWindowSize = Math.min(minWindowSize, windowEnd - windowStart + 1);
sumOfWindow -= A[windowStart];
windowStart++;
}
}
return minWindowSize;
}
}