public double findMaxAverage(int[] nums, int k) {
int n = nums.length;
int[] prefix = new int[n + 1];
int ans = Integer.MIN_VALUE;
for(int i = 1; i <= n; i++) {
prefix[i] = prefix[i - 1] + nums[i - 1];
}
for(int i = 0; i + k <= n; i++) {
ans = Math.max(ans, prefix[i + k] - prefix[i]);
}
return 1.0 * ans / k;
}
public int maxVowels(String s, int k) {
int n = s.length();
int[] prefix = new int[n + 1];
int ans = 0;
for(int i = 1; i <= n; i++) {
prefix[i] = prefix[i - 1] + isVowel(s.charAt(i - 1));
}
for(int i = 0; i + k <= n; i++) {
ans = Math.max(ans, prefix[i + k] - prefix[i]);
}
return ans;
}
private int isVowel(char c) {
char[] s = {'a','e','i','o','u'};
for(char sc: s) {
if(sc == c) return 1;
}
return 0;
}
public int longestOnes(int[] nums, int k) {
int ans = 0;
int l = 0, r = 0;
int n = nums.length;
int sum = 0;
int help = k;
while(r < n) {
// 右端点到最远距离
while(r < n && (nums[r] != 0 || help > 0)) {
if(nums[r] == 0) help--;
sum++; r++;
}
ans = Math.max(ans, sum);
// 收缩左端点
while(l < r && nums[l] != 0) {
l++;
sum--;
}
l++;
sum--;
help++;
}
return ans;
}
public int longestSubarray(int[] nums) {
int ans = 0, sum = 0;
int n = nums.length;
int l = 0, r = 0;
while(r < n) {
while(r < n && nums[r] == 1) {sum++; r++;}
// 特殊情况处理
if(r == n) sum--;
r++;
while(r < n && nums[r] == 1) {sum++; r++;}
ans = Math.max(ans, sum);
while(l < r && l < n && nums[l] == 1) {sum--; l++;}
l++;
}
return ans;
}