网易笔试(20210327)最长摇摆子数组长度

二、最长摇摆子数组长度

连续递增两次或者连续递减两次都算作摇摆。

连续递增两次以后必须递减,否则中断摇摆。

必须有两次递增递减才算完整的摇摆。

12321 这就是一次完整的摇摆

12322 只完成了递增,没有完成递减所以中断了

输入:

1 2 3 2 1

5

输出:

5

输入:

1 2 3 2 2

输出:0

import java.util.Scanner;

public class Wangyi_exam2 {
    // 最长摇摆子数组的长度
    // 摇摆数组:最少包含两次递增递减。递增:连续增加两次。递减连续减少两次。
    // 增加两次后必须减少两次。才算完成一次摇摆,如果出现不变则摇摆中断
    // 输入:1 2 3 2 1
    //       5
    //存在输出长度5,否则输出0

    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        String str1 = scanner.nextLine();
//        int n = Integer.parseInt(scanner.nextLine());
        // 模拟输入输出
        String str1 = "1 3 5 4 3 2 1 2 3 2 1 2 3 2 1";
        int n = 15;

        // 问题解决
        int max = solution(str1,n);

        // 输出问题
        System.out.println(max);
    }

    public static int solution(String str1, int n){
        String[] strs = str1.split(" ");
        // 0位置存放从0到1的状态,1增,-1减,0不变。
        int[] change = new int[n-1];
        for(int i=1; i<=n-1; i++){
            int v = Integer.parseInt(strs[i]) - Integer.parseInt(strs[i-1]);

            change[i-1] = v;
        }
        // 中断条件,①连续三次增/减。②减一次就增,或者增一次就减。③出现0(二三情况一样) ④超过长度
        // 中断后的起点位置。①当前位置减1变为新的起点。②当前位置变为新的起点。
        // 中断时更新最大长度max :最大长度为中断节点index-start,边界条件走到最后一个节点后面会中断。max大于等于5才会设置成功

        // 设置当前状态status(-2 -1 0 1 2)
        // 0状态可升可降,1只能升2,2后只能降为-1才能继续不中断 到零必中断。

        int max=0;
        int start=0;
        int status=0;

        int cur=0;
        while(cur < n-1){
            if(change[cur]>0){
                if(status == 0 || status == 1){
                    status++;
                }else if(status == 2){
                    // 出现中断条件1,三连增
                    // 更新最大长度
                    int c_s = cur-start + 1;
                    if(c_s>=5){
                        max = Math.max(max,c_s);
                    }
                    // 重置起点
                    start = cur-1;
                    // 状态变更
                    status = 2;

                }else if(status == -1){
                    // 出现中断条件2
                    // 更新最大长度
                    int c_s = cur-start + 1;
                    if(c_s>=5){
                        max = Math.max(max,c_s);
                    }
                    // 重置起点
                    start = cur;
                    // 状态变更
                    status = 0;

                }else{
                    // 顺利从-2 过度到1
                    status = 1;
                }

            }else if(change[cur]<0){
                if(status == 0 || status == -1){
                    status--;
                }else if(status == -2){
                    // 出现中断条件1,三连减
                    // 更新最大长度
                    int c_s = cur-start + 1;
                    if(c_s>=5){
                        max = Math.max(max,c_s);
                    }
                    // 重置起点
                    start = cur-1;
                    // 状态变更
                    status = -2;
                }else if(status == 1){
                    // 出现中断条件2
                    // 更新最大长度
                    int c_s = cur-start + 1;
                    if(cur-start>=5){
                        max = Math.max(max,c_s);
                    }
                    // 重置起点
                    start = cur;
                    // 状态变更
                    status = 0;
                }else{
                    // 顺利从2过度到-1;
                    status = -1;
                }

            }else{
                // 等于零必中断中断3和中断2类似。
                // 中断后当前摇摆长度大于5才会更新
                int c_s = cur-start + 1;
                if(c_s>=5){
                    max = Math.max(max,c_s);
                }
                // 更新start到中断节点
                start = cur;
                // 更新状态status
                status = 0;
            }
            cur++;
        }
        // 退出后再进行中断条件4 直接更新最大长度
        int c_s = cur-start + 1;
        if(c_s>=5) {
            max = Math.max(max, c_s);
        }
        return max;
    }
}
原文地址:https://www.cnblogs.com/wsZzz1997/p/14588367.html