-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMinAvgTwoSlice.java
31 lines (25 loc) · 908 Bytes
/
MinAvgTwoSlice.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
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// slices can only be 2 or 3 in length (mathematically)
// .. prefix sum at starting index - avg with next item
// .. can be dome in space
double minAvg = Double.MAX_VALUE;
int minAvgIndex = 0;
for(int i=0; i<A.length-1; i++) {
double avg = (A[i] + A[i+1]) / 2.0;
if(i < A.length - 2) {
double avgOf3 = (A[i] + A[i+1] + A[i+2]) / 3.0;
avg = avgOf3 < avg ? avgOf3 : avg;
}
if(minAvg > avg) {
minAvg = avg;
minAvgIndex = i;
}
}
return minAvgIndex;
}
}