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

43 篇文章 0 订阅
19 篇文章 0 订阅

我们把数组 A 中符合下列属性的任意连续子数组 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 的任意子数组,包括整个数组 A。)

给出一个整数数组 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));
}
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值