美团笔试真题——公司食堂

牛客网公司真题(笔试)系列

2024/3/24

美团2021校招笔试-编程题(通用编程试题,第10场)

一. 题目

在这里插入图片描述
在这里插入图片描述

二.思路

将1人桌和0人桌分别建立一个小顶堆,
遍历每个等待的人,
如果是男生,优先1人桌小顶堆;
如果是女生,优先0人桌小顶堆;

二. 自测通过,但提交超时
import java.util.Scanner;
import java.util.*;

//有一个坑是:输出桌号是1到n, 而不是0到n-1
//第二个坑是:0人桌占上一个之后,得把这个桌加入1人桌优先队列里
public class Main {
   
    public static void main(String[] args) {
   
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();  //有多少组数据
        for(int i = 0; i < t; i++){
   

            int n = in.nextInt();   //餐桌数
            int[] nums = new int[n];   //餐桌人数情况
            String str = in.next();
            for(int j = 0; j < n; j++){
   
                nums[j]  = str.charAt(j) - '0';
            }
            int m = in.nextInt(); //等待人数
            String str1 = in.next(); //等待男女情况
            char[] chars = new char[m];
            for(int k = 0; k < m; k++){
   
                chars[k]  = str1.charAt(k);
            }

            int[] result = test(n, nums, m, chars);
            for(int o = 0; o < m; o++){
   
                System.out.println(result[o]);
            }

        }
    }

    public static int[] test(int n, int[] nums, int m, char[] chars){
   
        int[] res = new int[m];

        //建两个队列是因为,2人桌已经满了,就没有可坐的位置了
        PriorityQueue<Integer> zeroTable = new PriorityQueue<>();
        PriorityQueue<Integer> oneTable = new PriorityQueue<>();

        //形成两个优先队列
        for(int i = 0; i < n; i++){
   
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值