题解 | #合唱队#

合唱队

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];
            int[] dp2 = new int[count];
            int max1=0;
            int max2=0;
            for (int i = 0; i < count; i++) {
                dp1[i]=1;
                for (int j = 0; j < i; j++) {
                    if(Integer.valueOf(str[i]) > Integer.valueOf(str[j])){
                        dp1[i]=Math.max(dp1[i],dp1[j]+1);
                    }
                }
                max1=Math.max(dp1[i],max1);
            }
            for (int i = count-1; i >= 0; i--) {
                dp2[i]=1;
                for (int j = count-1; j > i; j--) {
                    if(Integer.valueOf(str[i]) > Integer.valueOf(str[j])){
                        dp2[i]=Math.max(dp2[i],dp2[j]+1);
                    }
                }
                max2=Math.max(dp2[i],max2);
            }
            int max3=0;
            for (int i = 0; i < count; i++) {
                max3 = Math.max(dp1[i]+dp2[i]-1,max3);
            }
            System.out.println(count-max3);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

DBsan:我也遇到过好的HR,全程友好交流。这年头基本的礼貌和尊重为什么好多HR都做不到
找工作时遇到的神仙HR
点赞 评论 收藏
分享
09-20 22:39
中南大学
故事和酒66:意思就是用了AI辅助也不一定做得出来,还是有区分度,不然他不会让你用的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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