小米笔试9/19-装箱+排序挑战

第一题:装箱(动态规划)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int N = sc.nextInt(), n = sc.nextInt(), c = sc.nextInt(); // 容量 + 玩具个数 + 填充物个数
            int[] a = new int[n + c];
            for (int j = 0; j < n; j++) {
                a[j] = sc.nextInt();
            }
            for (int j = n; j < n + c; j++) {
                a[j] = 1;
            }
            boolean res = checkFull(N, n, a);

            if (res) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
        sc.close();
    }

    private static boolean checkFull(int N, int c, int[] a) {
        if (N <= c)
            return true;
        boolean[][] dp = new boolean[a.length][N + 1];
        if (a[0] <= N) {
            dp[0][a[0]] = true;
        }
        for (int i = 0; i < a.length; i++) {
            dp[i][0] = true;
        }
        for (int i = 1; i < a.length; i++) {
            for (int j = 1; j <= N; j++) {
                if (a[i] <= j) {
                    dp[i][j] = dp[i - 1][j - a[i]];
                }
                dp[i][j] = dp[i][j] || dp[i - 1][j];
            }
        }

        return dp[a.length - 1][N];
    }
}

第二题:排序挑战(贪心)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int n = sc.nextInt();
            int[] a = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = sc.nextInt();
            }
            int[] b = new int[n];
            for (int j = 0; j < n; j++) {
                b[j] = sc.nextInt();
            }
            boolean res = checkCanExchange(a, b, n);
            if (res) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
        sc.close();
    }

    private static boolean checkCanExchange(int[] a, int[] b, int n) {
        return checkUp(a, b) || checkUp(b, a) || checkDown(a, b) || checkDown(b, a);
    }

    private static boolean checkDown(int[] a, int[] b) {
        int n = a.length;
        int[] target = new int[n];
        int[] other = new int[n];
        for (int i = 0; i < n; i++) {
            target[i] = a[i];
            other[i] = b[i];
        }
        int pre = 10001;
        for (int i = 1; i < n; i++) {
            int minV = Math.min(target[i], other[i]);
            int maxV = Math.max(target[i], other[i]);
            if (maxV <= pre) {
                target[i] = maxV;
                pre = target[i];
            } else {
                if (minV <= pre) {
                    target[i] = minV;
                    pre = target[i];
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    private static boolean checkUp(int[] a, int[] b) {
        int n = a.length;
        int[] target = new int[n];
        int[] other = new int[n];
        for (int i = 0; i < n; i++) {
            target[i] = a[i];
            other[i] = b[i];
        }
        int pre = 0;
        for (int i = 0; i < n; i++) {
            int minV = Math.min(target[i], other[i]);
            int maxV = Math.max(target[i], other[i]);
            if (minV >= pre) {
                target[i] = minV;
                pre = target[i];
            } else {
                if (maxV >= pre) {
                    target[i] = maxV;
                    pre = target[i];
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

#小米笔试##25秋招#
全部评论
感谢佬
1 回复 分享
发布于 2024-09-19 17:37 山东
太厉害了,思路简单明了。
点赞 回复 分享
发布于 2024-09-20 11:35 安徽
第二题为什么要checkUp(a, b)又checkUp(b, a)呢,我看了下函数体这两个的结果应该是等效的吧
点赞 回复 分享
发布于 2024-09-20 11:00 四川
点赞 回复 分享
发布于 2024-09-19 22:07 湖北
膜拜佬
点赞 回复 分享
发布于 2024-09-19 21:51 江西
膜拜大佬!
点赞 回复 分享
发布于 2024-09-19 18:05 重庆
谢谢佬
点赞 回复 分享
发布于 2024-09-19 18:05 湖南
mark
点赞 回复 分享
发布于 2024-09-19 18:02 江西
大佬😊
点赞 回复 分享
发布于 2024-09-19 17:45 山东

相关推荐

评论
23
37
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11975次浏览 106人参与
# 你的实习产出是真实的还是包装的? #
2079次浏览 43人参与
# 巨人网络春招 #
11408次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7744次浏览 43人参与
# 简历第一个项目做什么 #
31835次浏览 345人参与
# 重来一次,我还会选择这个专业吗 #
433667次浏览 3926人参与
# 米连集团26产品管培生项目 #
6286次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187312次浏览 1122人参与
# 牛客AI文生图 #
21469次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152560次浏览 888人参与
# 研究所笔面经互助 #
118993次浏览 577人参与
# 简历中的项目经历要怎么写? #
310551次浏览 4230人参与
# AI时代,哪些岗位最容易被淘汰 #
64089次浏览 838人参与
# 面试紧张时你会有什么表现? #
30531次浏览 188人参与
# 你今年的平均薪资是多少? #
213254次浏览 1039人参与
# 你怎么看待AI面试 #
180338次浏览 1266人参与
# 高学历就一定能找到好工作吗? #
64352次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76650次浏览 374人参与
# 我的求职精神状态 #
448238次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363732次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160726次浏览 1112人参与
# 校招笔试 #
471657次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务