题解 | #合唱队#

合唱队

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        test24();
    }
    private static void test24() {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = Integer.parseInt(in.nextLine());
            String[] strs = in.nextLine().split(" ");

            int[] left = new int[n];
            for (int i = 0; i <= n - 1; i++) {
                left[i] = 1;
                for (int j = 0; j < i; j++) {
                    if (Integer.parseInt(strs[j]) < Integer.parseInt(strs[i])) {
                        left[i] = Math.max(left[i], left[j] + 1);
                    }
                }
            }
            int[] right = new int[n];
            for (int i = n - 1; i >= 0; i--) {
                right[i] = 1;
                for (int j = n - 1; j > i; j--) {
                    if (Integer.parseInt(strs[j]) < Integer.parseInt(strs[i])) {
                        right[i] = Math.max(right[i], right[j] + 1);
                    }
                }
            }

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

        }
    }
}

全部评论

相关推荐

Cons_W:我9本的,同样找不到。感觉是岗位太少的问题,可能12月份没多少岗位的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务