题解 | #合唱队#

合唱队

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

import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            int count = Integer.valueOf(br.readLine());
            String[] str = br.readLine().split(" ");
            int[] dp1 = new int[count + 1];
            int[] dp2 = new int[count + 1];
            int max1 = 0;
            int max2 = 0;
            for (int i = 1; i <= count; i++) {
                dp1[i] = 1;
                for (int j = 1; j < i; j++) {
                    if (Integer.valueOf(str[i - 1]) > Integer.valueOf(str[j - 1])) {
                        dp1[i] = Math.max(dp1[i], dp1[j] + 1);
                    }
                }
                max1 = Math.max(dp1[i], max1);
            }
            for (int i = count; i >= 1; i--) {
                dp2[i] = 1;
                for (int j = count; j > i; j--) {
                    if (Integer.valueOf(str[i - 1]) > Integer.valueOf(str[j - 1])) {
                        dp2[i] = Math.max(dp2[i], dp2[j] + 1);
                    }
                }
                max2 = Math.max(dp2[i], max2);
            }
            int max3 = 0;
            for (int i = 1; i <= count; i++) {
                max3 = Math.max(dp1[i] + dp2[i] - 1, max3);
            }
            System.out.println(count - max3);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
驼瑞驰_招募评论官版...:点击就挂,露头就秒
点赞 评论 收藏
分享
头像
09-01 09:00
已编辑
四川旅游学院 运营
牛客55195891...:主要是专业不好,别的没毛病
牛客解忧铺
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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