美团笔试8.10

第一题就是去重+排序

public class SecretKey {
    public static void main(String[] args) {
        Scanner  sc = new Scanner(System.in);
        List<Node> list = new ArrayList<>();
        int n = sc.nextInt();
        String ok = sc.next();
        HashSet<String> set = new HashSet<>();
        for(int i = 0;i<n;i++){
            String str = sc.next();
            if(!set.contains(str)){
                list.add(new Node(str.length(),str));
            }
            set.add(str);
        }
        int sum1 = 0;
        int sum2 = 0;
        list.sort(Comparator.comparingInt(Node::getKey));
        for(int i = 0;i<list.size();i++){
            if(list.get(i).getKey()<ok.length()){
                sum1++;
            }else{
                break;
            }
        }
        for(int i = 0;i<list.size();i++){
            if(list.get(i).getKey()<=ok.length()){
                sum2++;
            }else{
                break;
            }
        }
        System.out.println(sum1+1+" "+sum2);
    }
}
class Node {
    int key;
    String value;
    Node(int key,String value){
        this.key = key;
        this.value = value;
    }

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

第二题貌似就按题目去做就好了,最多就是计数排序?

public class DeleteArray {
    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();
            // 删除整组的花费系数
            long k = sc.nextLong();
            // 删除单个元素的花费
            long x = sc.nextLong();
            int [] nums = new int [n];
            int max = nums[0];
            for (int j = 0; j < n; j++) {
                nums[j] = sc.nextInt();
                max = Math.max(max, nums[j]);
            }
            int [] count = new int [max+1];
            long minValue = n*x;
            for (int j = n - 1; j >= 0; j--) {
                count[nums[j]] = 1;
                for (int k1 = 0; k1 < max; k1++) {
                    if (count[k1] == 0) {
                        long sum = j*x+k1*k;
                        minValue = Math.min(minValue, sum);
                        break;
                    }
                }
            }


            System.out.println(minValue);
        }
    }
}

第三题直接放弃,前面有些可能有小瑕疵,后来直接在牛客上面改了 大致如此,第二题主要是开个 long ,然后注意转化,空实例等问题就行了,一开始没开 long 通过率0,开完直接通过

面经(打怪升级系列) 文章被收录于专栏

打怪升级...

全部评论
第三题无限循环转化为长度2n的数组,超过了就取模,左右处理思路相同。然后问题变成查询若干个区间种类数,采用离线树状数组解决
2 回复 分享
发布于 2024-08-10 14:50 广东
大佬,能问一下这两个题的题目是什么吗
点赞 回复 分享
发布于 2024-08-17 16:20 黑龙江
好优雅
点赞 回复 分享
发布于 2024-08-10 17:41 上海

相关推荐

07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:10
直接上图
牛客13578115...:改得一般,不值80
点赞 评论 收藏
分享
评论
5
6
分享

创作者周榜

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