华为OD机试真题 - 执行任务赚积分

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int count = in.nextInt();
        int time = in.nextInt();
        LinkedList<int[]> runables = new LinkedList<>();
        for (int i = 0; i < count; i++) {
            int[] runable = new int[2];
            runable[0] = in.nextInt();
            runable[1] = in.nextInt();
            runables.add(runable);
        }
        System.out.println(getMaxScore(runables, time));
    }

    public static int getMaxScore(LinkedList<int[]> runables, int time) {
        Collections.sort(runables, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] == o2[0])
                    return o2[1] - o1[1];
                return o1[0] - o2[0];
            }
        });
        int score = 0;
        for (int i = 1; i < time; i++) {
            int[] runable = runables.pollFirst();
            if (runable[0] >= i) {
                score += runable[1];
                while (runables.peekFirst() != null &amp;&amp; runables.peekFirst()[0] == runable[0]) {
                    runables.pollFirst();
                }
            }
        }
        return score;
    }
全部评论

相关推荐

不愿透露姓名的神秘牛友
09-11 10:20
点赞 评论 收藏
分享
昨天 14:01
井冈山大学 Java
肖先生~:兄弟们,我发的她都点赞了,但是就是不给我微信
秋招被确诊为……
点赞 评论 收藏
分享
点赞 评论 收藏
分享
迷茫的大四🐶:hr:不好,他预判了我的预判
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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