PDD笔试题目题解

100+100+100+0.
感觉自己的代码质量贼差。

3.13 收到笔试通过。

import java.util.Arrays;
import java.util.Scanner;

/**
 * @ClassName PACKAGE_NAME.Main
 * @Desciption
 * @Author S
 * @DateTime 2019-03-10 16:34
 * @Version 1.0
 **/

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        for (int i = 0; i < n; i++) {
            b[i] = in.nextInt();
        }

        Arrays.sort(a);
        Arrays.sort(b);
        int sum = 0;

        for (int i = 0; i < n; i++) {
            sum += a[i] * b[n - i - 1];
        }
        System.out.println(sum);
    }
}

import java.util.*;

/**
 * @ClassName PACKAGE_NAME.Main
 * @Desciption
 * @Author S
 * @DateTime 2019-03-10 16:34
 * @Version 1.0
 **/

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        str = str.toLowerCase();
        if (str.length() <= 0) {
            System.out.println();
            return;
        }
        int[] record = new int[26];
        HashMap<Character, Integer> map = new HashMap<>();


        for (int i = 0; i < str.length(); i++) {
            map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1);
        }

        char ans = str.charAt(0);
        for (int i = 0; i < str.length(); i++) {
            if (map.get(str.charAt(i)) < 1) {
                ans = str.charAt(i);
                break;
            } else {
                // 这里有错,应该是样例简单才通过的,参见回帖
                if (i + 1 < str.length() && str.charAt(i+1) < str.charAt(i)) {
                    ans = str.charAt(i+1);
                    map.put(str.charAt(i), map.get(str.charAt(i)) - 1);
                } else {
                    ans = str.charAt(i);
                    break;
                }
            }
        }
        System.out.println(ans);
    }
}
import java.util.*;

/**
 * @ClassName PACKAGE_NAME.Main
 * @Desciption
 * @Author S
 * @DateTime 2019-03-10 16:34
 * @Version 1.0
 **/

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int d = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            b[i] = in.nextInt();
            map.put(a[i], b[i]);
        }
        Arrays.sort(a);
        for (int i = 0; i < n; i++) {
            b[i] = map.get(a[i]);
        }

        // i坐标 及 以后的最大的值
        int[] maxN = new int[n];
        maxN[n-1] = b[n-1];
        for (int i = n - 2; i >= 0; i--) {
            if (maxN[i+1] < b[i]) {
                maxN[i] = b[i];
            } else {
                maxN[i] = maxN[i+1];
            }
        }
        // 第i个银行,可选第二银行的范围;最后一个银行没有选择
        int[] indexI = new int[n];
        for (int i = 0; i < n; i++) {
            int j = 1;
            if (i != 0) {
                j = indexI[i-1];
            }
            for (; j < n; j++) {
                if (a[j] - a[i] >= d) {
                    indexI[i] = j;
                    break;
                }
            }
            if (j == n) {
                indexI[i] = -1;
                for (;i < n; i++) {
                    indexI[i] = -1;
                }
                break;
            }
        }
        int sum = 0;
        for (int i = 0; i < n - 1; i++) {
            if (indexI[i] == -1) {
                continue;
            }
            int tempSum = maxN[indexI[i]] + b[i];
            if (tempSum > sum) {
                sum = tempSum;
            }
        }
        System.out.println(sum);

    }
}

#笔试题目##题解##拼多多##春招#
全部评论
为什么我用两个for超时?
点赞 回复
分享
发布于 2019-03-10 22:49
大佬
点赞 回复
分享
发布于 2019-03-10 18:48
联想
校招火热招聘中
官网直投
niubility……
点赞 回复
分享
发布于 2019-03-10 18:49
第四题输出0可以有10%
点赞 回复
分享
发布于 2019-03-10 18:51
d大佬
点赞 回复
分享
发布于 2019-03-10 18:51
大佬
点赞 回复
分享
发布于 2019-03-10 18:52
大佬
点赞 回复
分享
发布于 2019-03-10 19:09
牛逼
点赞 回复
分享
发布于 2019-03-10 19:42
大佬,第二题的代码,如果测试用例是bdaBD怎么办,大佬的代码会输出b啊。
点赞 回复
分享
发布于 2019-03-10 20:00
第二题到底啥意思啊,不是输出代码里字典序里最小的吗?
点赞 回复
分享
发布于 2019-03-10 20:12
有大佬给看下题目么,菜鸡都没有机会答题,┭┮﹏┭┮
点赞 回复
分享
发布于 2019-03-10 20:35
T2的话用例是cdadc能过嘛🤔
点赞 回复
分享
发布于 2019-03-10 21:41
大佬咱俩第一题代码一模一样啊,会不会被查重。。
点赞 回复
分享
发布于 2019-03-10 23:10
第一题不是说,,,a可以排序。。。b不可以排序吗???我懵了
点赞 回复
分享
发布于 2019-03-11 14:43
第三题的解法很聪明呦~
点赞 回复
分享
发布于 2019-03-15 15:27

相关推荐

点赞 43 评论
分享
牛客网
牛客企业服务