日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Leetcode--845. 数组中的最长山脉

發(fā)布時間:2024/7/19 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode--845. 数组中的最长山脉 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們把數(shù)組 A 中符合下列屬性的任意連續(xù)子數(shù)組 B 稱為 “山脈”:

B.length >= 3
存在 0 < i?< B.length - 1 使得 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]
(注意:B 可以是 A 的任意子數(shù)組,包括整個數(shù)組 A。)

給出一個整數(shù)數(shù)組 A,返回最長 “山脈”?的長度。

如果不含有 “山脈”?則返回 0。

?

示例 1:

輸入:[2,1,4,7,3,2,5]
輸出:5
解釋:最長的 “山脈” 是 [1,4,7,3,2],長度為 5。
示例 2:

輸入:[2,2,2]
輸出:0
解釋:不含 “山脈”。
?

提示:

0 <= A.length <= 10000
0 <= A[i] <= 10000

思路:

1.遍歷每個點左邊的坡有多高

2.遍歷每個點右邊的坡有多高

3. 左右加起來比最大值

提交的代碼:

class Solution {
? ? public int longestMountain(int[] A) {
? ? ? ? if(A.length==0)
?? ??? ?{
?? ??? ??? ?return 0;
?? ??? ?}
? ? ? ? int left[] = new int[A.length];
? ? ? ? int right[] = new int[A.length];
? ? ? ? int max = 0;
? ? ? ? for(int i=1;i<A.length;i++)
? ? ? ? {
? ? ? ? ?? ?if(A[i]>A[i-1])
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?left[i]=left[i-1]+1;
? ? ? ? ?? ??? ?if(left[i-1]==0)
? ? ? ? ?? ??? ?{
? ? ? ? ?? ??? ??? ?left[i]++;
? ? ? ? ?? ??? ?}
? ? ? ? ?? ?}
? ? ? ? ?? ?else
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?left[i]=0;
? ? ? ? ?? ?}
? ? ? ? }
? ? ? ? for(int i=A.length-2;i>=0;i--)
? ? ? ? {
? ? ? ? ?? ?if(A[i+1]<A[i])
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?right[i]=right[i+1]+1;
? ? ? ? ?? ??? ?if(right[i+1]==0)
? ? ? ? ?? ??? ?{
? ? ? ? ?? ??? ??? ?right[i]++;
? ? ? ? ?? ??? ?}
? ? ? ? ?? ?}
? ? ? ? ?? ?else
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?right[i]=0;
? ? ? ? ?? ?}
? ? ? ? }
? ? ? ? for(int i=0;i<A.length;i++)
? ? ? ? {
? ? ? ? ?? ?if(left[i]!=0&&right[i]!=0)
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?max = Math.max(max, left[i]+right[i]);
? ? ? ? ?? ?}
? ? ? ? }
? ? ? ? if(max==0)
? ? ? ? {
? ? ? ? ?? ?return 0;
? ? ? ? }
? ? ? ? return max-1;
? ? }
}

完整的代碼:


public class Solution845 {
public static int longestMountain(int[] A) {
?? ??? ?if(A.length==0)
?? ??? ?{
?? ??? ??? ?return 0;
?? ??? ?}
? ? ? ? int left[] = new int[A.length];
? ? ? ? int right[] = new int[A.length];
? ? ? ? int max = 0;
? ? ? ? for(int i=1;i<A.length;i++)
? ? ? ? {
? ? ? ? ?? ?if(A[i]>A[i-1])
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?left[i]=left[i-1]+1;
? ? ? ? ?? ??? ?if(left[i-1]==0)
? ? ? ? ?? ??? ?{
? ? ? ? ?? ??? ??? ?left[i]++;
? ? ? ? ?? ??? ?}
? ? ? ? ?? ?}
? ? ? ? ?? ?else
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?left[i]=0;
? ? ? ? ?? ?}
? ? ? ? }
? ? ? ? for(int i=A.length-2;i>=0;i--)
? ? ? ? {
? ? ? ? ?? ?if(A[i+1]<A[i])
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?right[i]=right[i+1]+1;
? ? ? ? ?? ??? ?if(right[i+1]==0)
? ? ? ? ?? ??? ?{
? ? ? ? ?? ??? ??? ?right[i]++;
? ? ? ? ?? ??? ?}
? ? ? ? ?? ?}
? ? ? ? ?? ?else
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?right[i]=0;
? ? ? ? ?? ?}
? ? ? ? }
? ? ? ? for(int i=0;i<A.length;i++)
? ? ? ? {
? ? ? ? ?? ?if(left[i]!=0&&right[i]!=0)
? ? ? ? ?? ?{
? ? ? ? ?? ??? ?max = Math.max(max, left[i]+right[i]);
? ? ? ? ?? ?}
? ? ? ? ?? ?
? ? ? ? }
? ? ? ? if(max==0)
? ? ? ? {
? ? ? ? ?? ?return 0;
? ? ? ? }
? ? ? ? return max-1;
? ? }
public static void main(String[] args)
{
?? ?//int nums[] = {2,1,4,7,3,2,5};
?? ?int nums[] = {0,1,0};
?? ?System.out.println(longestMountain(nums));
}
}
?

總結

以上是生活随笔為你收集整理的Leetcode--845. 数组中的最长山脉的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。