forked from fu11211129/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxAvgSubarrayII.java
58 lines (44 loc) · 1.46 KB
/
MaxAvgSubarrayII.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class MaxAvgSubarrayII {
public static void main(String[] args) {
}
private boolean check(double est, int[] nums, int k) {
double sum = 0.0;
for(int i=0; i<k; ++i) sum += 1.0 * nums[i] - est;
// if sum of first leading k elements >= 0, meaning we find such a subarray whose sum >= 0.0
if(sum >= 0.0) return true;
double mi_pre_sum = 0;
double pre_k_sum = 0;
for(int i=k; i<nums.length; ++i) {
sum += 1.0 * nums[i] - est;
pre_k_sum += 1.0 * nums[i-k] - est;
mi_pre_sum = Math.min(mi_pre_sum, pre_k_sum);
// get the max sum.
if(sum >= mi_pre_sum) return true;
}
return false;
}
public double findMaxAverage(int[] nums, int k) {
if(nums == null || nums.length == 0) return 0;
int n = nums.length;
double mx = Double.MIN_VALUE, mi = Double.MAX_VALUE;
for(int x: nums) {
mx = Math.max(mx, (double)x);
mi = Math.min(mi, (double)x);
}
if(mx == mi) return mx;
double err = Double.MAX_VALUE;
double pre_est = mx;
double est = 0.0;
while(err > 0.00001) {
est = (mx + mi) * 0.5;
if(check(est, nums, k)) {
mi = est;
} else {
mx = est;
}
err = Math.abs(est - pre_est);
pre_est = est;
}
return mx;
}
}