美团笔试 九月二号 分享一下后三题

第五题,给出一列数两两凑对能凑出多少不同的对,没思路用优先队列瞎写居然过了,有人能证明最优解吗

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        HashMap<Integer,Integer> map = new HashMap<>();
        int n = in.nextInt();
        in.nextLine();
        for(int i = 0;i < n;i++) {
            int t = in.nextInt();
            map.put(t,1 + map.getOrDefault(t,0));
        }
        int m = map.size();
        PriorityQueue<Integer> pq = new PriorityQueue<>(((o1, o2) -> (o2 - o1)));
        for(int e:map. values()){
            pq.offer(e);
        }
        int res = 0;

        while (!pq.isEmpty()){
            List<Integer> temp = new ArrayList<>();
            int cur = pq.poll();
            while (cur > 0 && !pq.isEmpty()){
                if(pq.size() == 1 && cur == 2) {
                    cur = 0;
                    res ++;
                    continue;
                }
                cur--;
                res++;
                int nxt = pq.poll() - 1;
                if(nxt > 0) temp.add(nxt);
                
            }
            if(pq.isEmpty() && cur >= 2) res ++;
            pq.addAll(temp);
        }
        System.out.println(res);


    }
}

第四题,给出n个数删除k个使得剩下的互相为倍数,动态规划

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt(),k = in.nextInt();
        in.nextLine();
        int[] nums = new int[n];
        for(int i = 0;i < n;i++) {
            nums[i] = in.nextInt();
        }
        Arrays.sort(nums);
        int mod = 1000000007;
        int res = 0;
        int[][] f = new int[n][n - k];
        for(int i = n - 1;i >= 0;i--){
            f[i][0] = 1;
            for(int j = i + 1;j < n;j++){
                if(nums[j] % nums[i] == 0) {
                    for(int p = 1;p <n - k;p++){
                        f[i][p] = (f[i][p] + f[j][p - 1]) % mod;
                    }
                }
            }
            res = (res + f[i][n - k - 1]) % mod;
        }
        System.out.println(res);

    }
}

第三题:给n个数,每次把一个数除2或把第一个数乘2,求让第一个数成为最大的最少操作数

import java.util.PriorityQueue;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        in.nextLine();
        int first = in.nextInt();
        if(n == 1) {
            System.out.println(0);
            return;
        }
        PriorityQueue<Integer> pq = new PriorityQueue<>(((o1, o2) -> (o2 - o1)));
        for(int i = 1;i < n;i++) pq.offer(in.nextInt());
        int res = Integer.MAX_VALUE,cur = 0;
        while (cur < res){
            int p = pq.poll();
            res = Math.min(count(p,first) + cur,res);
            pq.offer(p / 2);
            cur++;
        }
        System.out.println(res);
    }
    public static int count(int t,int f){
        int res = 0;
        while (f < t){
            f *= 2;
            res ++;
        }
        return res;
    }
}

全部评论
第五题我直接输出n/2,ac了
3 回复 分享
发布于 2023-09-02 21:19 江西
想问一下之前有刷到类似的题目吗?还是就是当场做出来的?
点赞 回复 分享
发布于 2023-09-03 16:43 英国
while循环里的内容该怎么理解呀?
点赞 回复 分享
发布于 2023-09-03 11:31 湖南
校友牛!
点赞 回复 分享
发布于 2023-09-02 22:41 陕西
同样是电专 ,为啥你如此优秀?
点赞 回复 分享
发布于 2023-09-02 21:24 陕西

相关推荐

刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
12
15
分享

创作者周榜

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