9.6 去哪儿笔试 开发

第一题:字符数组字典序 100%

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(reader.readLine());
        String[] as = reader.readLine().split(" ");

        Arrays.sort(as, (s1, s2) -> (s1 + s2).compareTo(s2 + s1));

        for (String str : as) {
            System.out.printf("%s ", str);
        }
    }
}

第二题:二分 + 贪心 100%

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] inputs = reader.readLine().split(" ");
        int n = Integer.parseInt(inputs[0]);
        long m = Integer.parseInt(inputs[1]);
        int[] a = new int[n];
        String[] as = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) a[i] = Integer.parseInt(as[i]);
        int[] b = new int[n];
        String[] bs = reader.readLine().split(" ");
        for (int i = 0; i < n; ++ i) b[i] = Integer.parseInt(bs[i]);

        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = l + (r - l) / 2;

            if (check(mid, a, b, m)) {
                r = mid - 1;
            }
            else l = mid + 1;
        }

        System.out.println(l >= n ? -1 : (l + 1));
    }

    private static boolean check(int k, int[] a, int[] b, long limit) {
        int[] a_copy = Arrays.copyOfRange(a, 0, k + 1);
        Arrays.sort(a_copy);
        int[] b_copy = Arrays.copyOfRange(b, 0, k + 1);
        Arrays.sort(b_copy);

        long t = 0L;
        for (int i = k; i >= 0; -- i) {
            t += (long) a_copy[i] * b_copy[i];
            if (t >= limit) return true;
        }
        return false;
    }
}

第三题 没看

#去哪儿旅行秋招#
全部评论

相关推荐

06-27 15:15
长安大学 Java
哈哈哈,你是老六:这种就是培训机构骗钱的
点赞 评论 收藏
分享
lllllkin:感觉可以精简到一页简历,有些排版感觉不是必须的。 时间线越早的,你自己越熟悉的放前面。描述可以更精简些,一些问题解决感觉可以不用写具体技术栈,卖个关子,等面试官问。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
2
7
分享

创作者周榜

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