You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
667 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class_2022_03_4_week;
// 来自学员问题
// 给定一个数字n表示一开始有编号1~n的树木列成一条直线
// 给定一个有序数组arr表示现在哪些树已经没了arr[i]一定在[1,n]范围
// 给定一个数字m表示你可以补种多少棵树
// 返回补种之后,最长的连续树木,有多少棵
public class Code06_LongestContinuousTrees {
public static int longestTrees(int n, int m, int[] arr) {
int ans = 0;
int start = 1;
for (int i = 0, j = m; j < arr.length; i++, j++) {
ans = Math.max(ans, arr[j] - start);
start = arr[i] + 1;
}
ans = Math.max(ans, n - start + 1);
return ans;
}
}