题解 | #合唱队#

合唱队

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int sum = Integer.parseInt(br.readLine());
        String[] heightStr =  br.readLine().split(" ");
        int[] heights = Arrays.asList(heightStr).stream().mapToInt(Integer::parseInt).toArray();
        boolean tag = false;
        int count = 0;
        int ht = heights[0];
        int[] left = new int[heights.length];
        int[] right = new int[heights.length];
        for (int i = 0,j = heights.length - 1; i < sum && j >= 0; i++,j--) {
            left[i] = 1;
            right[j] = 1;
            for (int m=0,n=heights.length-1;m<i && n>j;m++,n--){
                if (heights[i] > heights[m]){
                    left[i] =  Math.max(left[m]+1,left[i]);
                }
                if (heights[j] > heights[n]){
                    right[j] = Math.max(right[n]+1,right[j]);
                }
            }
        }
        int max = left[0] + right[0] -1;
        for (int i = 0; i < left.length; i++) {
            if ((left[i] + right[i] -1) > max){
                max = left[i] + right[i] -1;
            }
        }

        System.out.println(heights.length - max);
    }

}

全部评论

相关推荐

06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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