Skip to content

Instantly share code, notes, and snippets.

@clarkdo
Created January 26, 2018 06:37
Show Gist options
  • Save clarkdo/081aba40c8c7b1a1f75209e54da96523 to your computer and use it in GitHub Desktop.
Save clarkdo/081aba40c8c7b1a1f75209e54da96523 to your computer and use it in GitHub Desktop.
class Solution {
public int solution(int[] A) {
int result = 0;
double best = Double.MAX_VALUE;
for (int i = 0; i < A.length - 1; i++) {
int sum = 0;
int increase = 0;
for (int j = i; j < A.length; j++) {
sum += A[j];
int count = j - i;
if (count > 0) {
double average = sum / (count + 1.0);
if (average < best) {
increase = 0;
best = average;
result = i;
} else if (++increase > 1) {
break;
}
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment