题解 | #合唱队#

合唱队

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String a, b;
        char[] chs;
        int i = 0, j = 0, n = 0, choir;
        try {
            a = r.readLine();
            chs = a.toCharArray();
            while (i < chs.length) {
                n = n * 10;
                n = n + chs[i] - '0';
                i++;
            }
            b = r.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int[] height = new int[n];
        int[] numLt = new int[n];
        int[] numRt = new int[n];
        i = 0;
        n = 0;
        chs = b.toCharArray();
        while (i < chs.length) {
            if (chs[i] == ' ') {
                height[j++] = n;
                n = 0;
                i++;
                continue;
            }
            n = n * 10;
            n = n + chs[i] - '0';
            if (i == chs.length - 1) height[j] = n;//将身高数组赋值
            i++;
        }
        i = 0;
        while (i < numLt.length) {//赋初始值1
            numLt[i] = 1;
            numRt[i] = 1;
            i++;
        }
        i = 1;
        while (i < numLt.length) {//从左到右,左边比该位小的队列
            j = 0;
            while (j < i) {
                if (height[j] < height[i] && numLt[j] + 1 > numLt[i]) numLt[i] = numLt[j] + 1;
                j++;
            }
            i++;
        }
        i = numRt.length - 2;
        while (i >= 0) {//从右到左,右边比该位小的队列
            j = numRt.length - 1;
            while (j > i) {
                if (height[j] < height[i] && numRt[j] + 1 > numRt[i]) numRt[i] = numRt[j] + 1;
                j--;
            }
            i--;
        }
        i = 0;
        n = 0;//表示合唱队形最大的人数,左边递增加右边递减,然后减去自身
        while (i < numLt.length) {
            choir = numLt[i] + numRt[i] - 1; //合唱队人数
            if (choir > n) n = choir;
            i++;
        }
        System.out.print(height.length - n);
    }
}

全部评论

相关推荐

好消息是活的像个人了,周末可以约会吃饭打游戏了坏消息是钱没了,当初来小红书就是为了钱啊哭笑不得😭
犯困嫌疑人:好事儿啊,取消大小周能有更多自己的时间,周末还能约对象玩,这不美滋滋?
投递小红书等公司8个岗位 > 小红书取消大小周
点赞 评论 收藏
分享
牛客10001:问就是六个月,全国可飞,给钱就干
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务