• Java实现 数组匹配,矩阵行排序


    分两个长度相等的经致数组 persons 和 prizes,其中 persons[i] 是初始队列里第 i 名员工欢的奖品 (i =0 是队首位置), prizes[j],这个过程会一直持续到队列没有员工,或所有剩余员工都不喜欢最顶上的奖品为止。是第了个奖品的英型(j0表示最顶上)请适回无法取到自己密欢的奖品的员工致量。
    lass
    示例1
    输入 : persons = [0,1,1,0,1], prizes = [1,0,0,1,0]
    队首的员工放奔最顶上的奖品,并回到队列的末尾,队列变为 persons = [1,1,0,1,@]。
    输出:队首的员工拿走最顶上的奖品,并离开队列,队列变为 persons = [1,0,1,0],奖品为 prizes = [o,0,1,0]的释:队首的员工放弃最顶上的奖品,并回到队列的末尾,队列变为 persons = [,1,0,1]。队首的员工拿走最顶上的奖品,并离开队列,队列变为 persons = [1,0,1],奖品为 prizes = [e,1,0]。队首的员工放奔最顶上的奖品,并回到队列的末尾,队列变为 persons = [e,1,1]。队首的员工拿走最顶上的奖品,并离开队列,队列变为 persons = [1,1],奖品为 prizes = []e]。-队首的员工拿走最顶上的奖品,并离开队列,队列变为 persons = [1],奖品为 prizes = [e]。最后剩余的 名员工不喜欢最顶上的奖品,因此返回 1。
    示例2:
    输入: persons = [1,0,0,1,0], prizes = [0,0,1,0,1]
    输出:0

    题目是识别的,凑合着看吧

    private static int getNumWithoutPrize(int[] persons,int[] prizes){
    //        System.out.println("------------------------------");
    //        int res = 0;
            Queue<Integer> que = new LinkedList();
            int pPrize = 0;
            //遍历person
            for(int pers = 0; pers<persons.length; pers++){
                //没找到想要的礼物
                if(persons[pers] != prizes[pPrize]){
                    que.add(persons[pers]);
                }else{
                    //找到
                    pPrize++;
                    continue;
                }
            }
            int queSize = 0;
            int i=0;
            while(!que.isEmpty()){
                int tmp = (int)que.poll();
                queSize = que.size();
    
                if(tmp == prizes[pPrize]){
                    pPrize++;
                    i=0;
                }else{
                    i++;
                    que.add(tmp);
                    if(queSize<=i)
                        break;
                }
            }
    
            return que.size();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    解目:因为同一型门的所有员工必须乘同一辆车,所以必须选择核载人数大于等于部门人数的车辆局出:0
    日斯目法述
    1 万至 plans0] 最少的要空位为: (12-10)+(8-8)+(15-15)=2dept0]10选用plans0]2]的车型,其核人数为12,大于等于10opt(1]e8这用plans(0[0]的车型,其核载人数为8,大于等于8lepts2]15选用 plans(0]1]的车型,其核载人数为 15大等于15空位为: (15-10)+(15 8)+(15-15)= 12注意:1)只使用了核载人数 15 的车型(不同部门可租同一型号的车),其他车型未使用方至plans(1]最少的deptsl1]两个部门,不能搭乘在一个核载人数为20的车上2)不允许扔车,如:depts(0方 plans2] 最少的总空位为:(15-10)+(8-8)+(15-15)=5方案0、1、2的总空位分别是2、12、5,因此选择 plans[0],返回下标0
    东例2
    融人: depts =[5, 9]plans = [[4],[6, 10),[5, 11]
    称出:1)方 plans(0]无法满足搭乘条件2)方 plansl1]最少的空位为: (6-5)+(10-9)=2:3)方 plans/2]最少的总空位为: (5-5)+(11-9)=2plans[1]和 plans[2]的最少空位均为 2,返回其中下标最小的 1
    示例3;
    输入: depts= [10,10]plans = [[2, 9,8, 31.[7]]
    解释:由于同一部门的所有员工需要搭乘同一辆车,不允许将同一部门的员工拆分乘坐,所给方案中最大的车型核载人数不足 10人,因此没有符合要
    输出: -]
    求的方案,返回-1。

    private static int rentCars(int[] depts,int[][] plans){
            int res = 0;
            Arrays.sort(depts);//对部门升序排序
            int plansNum = plans.length;
            for(int i=0;i<plansNum;i++)
                Arrays.sort(plans[i]);//对方案升序排序
    
            int[] min = new int[plansNum];
            for(int i=0;i<min.length;i++){
                min[i] = Integer.MAX_VALUE;
            }
    
            //遍历方案
            for(int i=0;i<plansNum;i++){
                int pPlan = 0;//每次方案指针重置为0
                int pDep = 0;//部门同
                while(pDep < depts.length && pPlan<plans[i].length){
                    //<= 坐得下的情况
                    if(depts[pDep] <= plans[i][pPlan]){
                        int tmp = plans[i][pPlan] - depts[pDep];
                        if(min[i]==Integer.MAX_VALUE){
                            min[i] = tmp;
                        }else{
                            min[i] += tmp;//统计差值
                        }
                        pDep++;//下一个部门
                    }else{
                        //坐不下
                        pPlan++;//考虑下一个更大的车
                    }
                }
            }
    
            int tmp = Integer.MAX_VALUE;
            for(int i=0;i<min.length;i++){
                if(min[i]<tmp){
                    tmp = min[i];
                    res = i;
                }
            }
    
            return  res;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    常用sql语句
    WorldView卫星遥感影像数据/米级分辨率遥感影像
    java计算机毕业设计简易在线教学系统源码+数据库+系统+lw文档+mybatis+运行部署
    在LangChain中使用Milvus + openai使用
    Spring Boot 链路追踪 SkyWalking 入门
    【环境】我决定半场开香槟!ubuntu20.04 安装 pytorch
    Element-ui配合vue上传图片
    Apache Maven Assembly自定义打包插件的使用
    【迁移学习】分布差异的度量以及迁移学习的统一表征方法
    Python少儿编程小课堂(六)入门篇(6)
  • 原文地址:https://blog.csdn.net/qq_41550190/article/details/127849836