/**
* @author xnl
* @Description:
* @date: 2022/8/17 23:28
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public int longestMountain(int[] arr) {
int n = arr.length;
if (n == 0){
return 0;
}
int[] left = new int[n];
for (int i = 1; i < n; i++){
left[i] = arr[i - 1] < arr[i] ? left[i - 1] + 1 : 0;
}
int[] right = new int[n];
for (int i = n - 2; i >= 0; i--){
right[i] = arr[i + 1] < arr[i] ? right[i + 1] + 1 : 0;
}
int ans = 0;
for (int i = 0; i < n; i++){
if (left[i] > 0 && right[i] > 0){
ans = Math.max(ans, left[i] + right[i] + 1);
}
}
return ans;
}
}
力扣:845. 数组中的最长山脉
于 2022-08-17 23:48:18 首次发布
这个Java程序定义了一个名为`Solution`的类,其中包含一个`longestMountain`方法,用于找出给定整数数组中最长的山脉长度。山脉是指数组中从一个上升到最高点然后下降的部分。程序首先初始化左右两个数组来存储左侧和右侧升序或降序的长度,然后遍历数组以找到最长的山脉。

328

被折叠的 条评论
为什么被折叠?



