845. 数组中的最长山脉

845. 数组中的最长山脉

我们把数组 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

思路

想法很重要啊,我用的栈,先找上坡,再找下坡,打死不百度;呵呵,TMD(说粗话不会怎么样把),总有不诚信如意的情况发生,然后就产生了各种各样的if else;虽然最后通过了,但是看了下通过后排名最高的方法,就是下面第二段程序,那叫一个简单,怎么就没想到呢。扫描数组,找一个左右两边都小于它的位置,继续往左右搜索就行了。

自己的辣鸡代码

#include<algorithm>
class Solution {
public:
    int longestMountain(vector<int>& A) {
        int judge=0,max_r=0,i=0;
        vector<int> tp;
        while(i<A.size()){
            if(tp.empty()){tp.push_back(A[i]);}
            else if(judge==0 && A[i]>tp[tp.size()-1]){
                tp.push_back(A[i]);
            }
            else if(tp.size()>1 && A[i]<tp[tp.size()-1]){
                judge=1;
                tp.push_back(A[i]);
                if(i==A.size()-1){
                    int si = tp.size();
                    max_r = std::max(si,max_r);
                    return max_r;
                }
            }
            else if(judge==1 && A[i]>tp[tp.size()-1]){
                int si = tp.size();
                cout<<si<<"  "<<i<<"  "<<tp[0]<<"  "<<tp[1]<<tp[tp.size()-1]<<endl;
                max_r = std::max(si,max_r);
                tp.clear();//clear不仅删除,容器大小也变为0
                tp.push_back(A[i-1]);
                tp.push_back(A[i]);

                judge=0;
            }
            else if(judge==1 && A[i]==tp[tp.size()-1]){
                int si = tp.size();
                cout<<si<<"  "<<i<<"  "<<tp[0]<<"  "<<tp[1]<<tp[tp.size()-1]<<endl;
                max_r = std::max(si,max_r);
                tp.clear();//clear不仅删除,容器大小也变为0
                tp.push_back(A[i]);

                judge=0;
            }
            else {
                judge=0;
                tp.clear();
                tp.push_back(A[i]); //记得把这个push进去作为下一轮的开始
            }

            i++;
        }
        return max_r;
    }
};

大神的方法

class Solution {
public:
    int longestMountain(vector<int>& A) {
        if(A.empty()) return 0;
        int res=0;
        for(int i=1; i<A.size()-1; i++)
        {
            if(A[i]>A[i-1] && A[i]>A[i+1])
            {
                int l=i-1, r=i+1;
                while(l>0)
                {
                    if(A[l]>A[l-1])
                        l--;
                    else
                        break;
                }
                while(r<A.size()-1)
                {
                    if(A[r]>A[r+1])
                        r++;
                    else
                        break;
                }
                res = max(res,r-l+1);
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值