/**
* @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 首次发布