forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2256.java
40 lines (39 loc) · 1.37 KB
/
_2256.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
34
35
36
37
38
39
40
package com.fishercoder.solutions;
public class _2256 {
public static class Solution1 {
public int minimumAverageDifference(int[] nums) {
if (nums.length == 1) {
return 0;
}
long secondHalfSum = 0;
int minAveDiff = Integer.MAX_VALUE;
for (int i = 1; i < nums.length; i++) {
secondHalfSum += nums[i];
}
long firstHalfSum = nums[0];
int count = 1;
int minDiffIndex = 0;
for (int i = 0; i < nums.length; ) {
int firstHalfAve = (int) (firstHalfSum / count);
int secondHalfAve = 0;
if ((nums.length - count) != 0) {
secondHalfAve = (int) (secondHalfSum / (nums.length - count));
}
if (minAveDiff > Math.abs(firstHalfAve - secondHalfAve)) {
minAveDiff = Math.abs(firstHalfAve - secondHalfAve);
minDiffIndex = i;
if (minAveDiff == 0) {
return minDiffIndex;
}
}
count++;
i++;
if (i < nums.length) {
firstHalfSum += nums[i];
secondHalfSum -= nums[i];
}
}
return minDiffIndex;
}
}
}