java解答
思路:在滑动窗口内保证补种树大于等于死的树的前提下得到窗口长度的最大值。
package test;标签:end,试题,temp,int,max,---,start,华为,scanner From: https://www.cnblogs.com/blogbook/p/16814999.html
/**
* Description:
* Author: Mr.Zhao
* Create Date Time: 2022/10/21 21:19.
* Update Date Time:
*/
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//总数
int N = Integer.parseInt(scanner.nextLine());
//未成活的树
int M = Integer.parseInt(scanner.nextLine());
// 未成活的具体树木---用数组表示
String[] Ms = scanner.nextLine().split(" ");
int[] ints_M = new int[M];
//补种的树木K
int K = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < Ms.length; i++) {
ints_M[i] = Integer.parseInt(Ms[i]);
}
int start=1;
int end=1;
int max=0;
int temp=0;
while (start <= end && start <= N) {
int same=0;
for (int i = start; i <=end; i++) {
for (int j = 0; j < ints_M.length; j++) {
if (i == ints_M[j]) {
same++;
}
}
}
if (end <= N && same <= K) {
temp = end-start+1;
max = Math.max(max, temp);
end =end+1;
} else {
start++;
if (start <= end && same <= K) {
temp = end-start+1;
max = Math.max(max, temp);
}
}
}
System.out.println(max);
}
}