1 Star 0 Fork 2

Emotion404 / JavaBooks

forked from 帝八哥 / JavaBooks 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
laohu.md 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
DreamCats 提交于 2020-08-09 19:01 . 更新笔试题

题目忘记了,改天补上。

求最小车辆

class Main2 {
    public static void main(String[] args) {
        int[][] trips = {{2, 1, 4}, {4, 2, 6}, {6, 6, 8}};
        int capacity = 5;
        System.out.println(minCarCount(trips, capacity));
    }

    public static int minCarCount (int[][] trips, int capacity) {
        // write code here
        Arrays.sort(trips, (a, b) -> a[2] - b[2]);
        int n = trips.length;
        int cnt = trips[0][0] <= capacity ? 1 : trips[0][0] / 5 + 1;
        int e = trips[0][2];
        for (int i = 1; i < n; i++) {
            if (trips[i][1] != e) {
                cnt += trips[i][0] <= capacity ? 1 : trips[i][0] / 5 + 1;
            } else {
                cnt += trips[i][0] <= capacity ? 0 : trips[i][0] / 5;
            }
            e = trips[i][2];
        }
        return cnt;
    }
}

做任务升级,求最大级别

class Main1 {
    public static void main(String[] args) {

        int x = 2;
        int leval = 1;
//        int[][] tasks = {{0,1}, {1, 2}, {1, 3}};
        int[][] tasks = {{5,15}, {5, 10}, {1, 4}};
        int ret = maxLevel(x, leval, tasks);
        System.out.println(ret);
    }
    public static int maxLevel (int x, int level, int[][] tasks) {
        // write code here
        Arrays.sort(tasks, (a, b) -> a[1] == b[1] ? a[0] - b[0] : b[1] - a[1]);
        int n = tasks.length;
        int nLevel = level;
        boolean flag = false;
        Queue<int[]> queue = new LinkedList<>();
        if (x == 0)
            return level;
        for (int i = 0; i < n; i++) {
            if (nLevel >= tasks[i][0]){
                nLevel += tasks[i][1];
                x--;
                if (x == 0)
                    break;
                int size = queue.size();
                while (size-- > 0){
                    int[] tmp = queue.peek();
                    if (nLevel >= tmp[0]) {
                        nLevel += tmp[1];
                        queue.poll();
                        x--;
                    }
                    if (x == 0){
                        flag = true;
                        break;
                    }
                }
            } else {
                queue.add(tasks[i]);
            }
            if (flag)
                break;
        }
        if (queue.size() == n)
            return level;
        return nLevel;
    }
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Emotion404/JavaBooks.git
git@gitee.com:Emotion404/JavaBooks.git
Emotion404
JavaBooks
JavaBooks
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891