java开发美团二面算法题

1.题目:

2.代码

链接:https://www.nowcoder.com/questionTerminal/4e1012fe691b446d88eba5db8f511692
来源:牛客网
 

import java.util.*;

public class test20170522F {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);

        while (sc.hasNext()) {

            int N = sc.nextInt();

            int[] a = new int[N];

            int[] maxLen = new int[N];

            for(int i=0;i<N;i++){

                a[i] = sc.nextInt();

            }

            //对a[0]特殊处理

            if (a[1] >= 2) {

                maxLen[0] = 2;

                for (int i = 2; i < N; i++) {

                    if (a[i] > a[i - 1])

                        maxLen[0]++;

                    else

                        break;

                }

            }

            //对a[N-1]特殊处理

            maxLen[N - 1] = 2;

            for (int i = N - 2; i >= 0; i--) {

                if (a[i] > a[i - 1])

                    maxLen[N - 1]++;

                else

                    break;

            }

            //对标号是1~N-2的进行处理

            for (int i = 1; i <= N - 2; i++) {

                if (a[i + 1] - a[i - 1] >= 2) {

                    maxLen[i] = 3;

                    for (int j = i - 1; j > 0; j--) {

                        if (a[j] > a[j - 1])

                            maxLen[i]++;

                        else

                            break;

                    }

                    for (int j = i + 1; j < N-1; j++) {

                        if (a[j + 1] > a[j])

                            maxLen[i]++;

                        else

                            break;

                    }

                } else

                    maxLen[i] = 2;

            }

            Arrays.sort(maxLen);

            System.out.println(maxLen[N-1]);

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值