题解 | #合唱队#

合唱队

https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int length = in.nextInt();
            int[] arr = new int[length];

            for (int i = 0; i < length; i++) {
                arr[i] = in.nextInt();
            }

            int[] left = new
            int[length]; // 每个节点向左递减最大的子序列长度
            int[] right = new
            int[length];// 每个节点向右递减最大的子序列长度

            // 初始化两个数列,全设置为1
            for (int i = 0; i < length; i++) {
                left[i] = 1;
                right[i] = 1;
            }

            for (int i = 0; i < length; i++) {
                for (int j = 0; j < i; j++) {
                    if (arr[i] > arr[j]) {
                        left[i] = Math.max(left[i], left[j] + 1);
                    }
                }
            }

            for (int i = length - 1; i > -1; i--) {
                for (int j = i + 1; j < length; j++) {
                    if (arr[i] > arr[j]) {
                        right[i] = Math.max(right[i], right[j] + 1);
                    }
                }
            }

            int maxNum = 0;
            int[] tempArr = new int[length];
            for (int i = 0; i < length; i++) {
                tempArr[i] = left[i] + right[i] - 1;
                maxNum = Math.max(tempArr[i], maxNum);
            }
            System.out.println(length - maxNum);

        }
        in.close();
        ;
    }
}

全部评论

相关推荐

吴offer选手:我卡在笔试才是最好笑的,甚至没给我发过笔试链接
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务