【华为OD题库-024】组装最大可靠性设备-java

题目

一个设备由N种类型元器件组成(每种类型元器件只需要一个, 类型type编号从0~N-1);每个元器件均有可靠性属性reliability,可靠性越高的器件其价格price越贵。而设备的可靠性由组成设备的所有器件中可靠性最低的器件决定。给定预算S,购买N种元器件(每种类型元器件都需要购买一个), 在不超过预算的情况下,请给出能够组成的设备的最大可靠性。
输入描述

S N // S总的预算,N元器件的种类

total //元器件的总数,每种型号的元器件可以有多种:

此后有total行具体器件的数据

type reliability price //type整数类型,代表元器件的类型编号从0 ~ N-1; reliabily整数类型,代表元器件的可靠性: price 整数类型,代表元器件的价格。
输出描述

符合预算的设备的最大可靠性,如果预算无法买产N种器件,则返回-1
备注

0 <= S,price <= 10000000

0<=N<= 100

0 <=type <= N-1

0 <= total <= 100000

0 < reliability <= 100000
示例1:
输入

500 3

0 80 100

0 90 200

1 50 50

1 70 210

2 50 100

2 60 150
输出

60
说明

预算500,设备需要3种元件组成,方案类型0的第一个(可靠性80),

类型1的第二个可靠性70).

类型2的第二个(可靠性60).

可以使设备的可靠性最大60
示例2:
输入

100 1

0 90 200
输出

-1

说明

组成设备需要1个元件,但是元件价格大于预算,因此无法组成设备,返回-1

思路

组合题,从total个元器件中选取N个,使其价格不超过S,求满足条件的组合中可靠性的最大值(可靠性由组合中最低的元器件可靠性决定)

这类组合问题的大概思路可以参考另一篇博文:【JAVA-排列组合】一个套路速解排列组合题

针对本题来说,注意以下几点:

  1. 不再是对简单的int进行选取,而是一个java对象,新建Device对象,含有type,price,reliability三个属性
  2. 同一个类型不能重复选择,要选取N个元器件,测试数据也只会给出N种元器件(每种型号的元器件可以有多种),且其type编号是0~N-1。所以还是可以使用一个int数组标识某种元器件是否已被使用。int[] used = new int[N]; 被使用时标记:used[devices[i].getType()] = 1;此时应该剪枝。
  3. 可靠性计算方法为path中可靠性最低的元器件,所以每次进行计算时,可靠性的表达式应该为:Math.min(reliability, devices[i].getReliability())
  4. 题目只需要求最终最大的可靠性,不需要输出具体的组合,所以不需要在外层设置变量res存放各种组合的结果。只需要判断,当path长度等于N,并且价格不超过预算时,其可靠性是否变得更大,最终输出满足条件的最大的可靠性即可

题解

java 复制代码
package hwod;

import java.util.*;

public class LargestReliability {
    private static int ans = -1;//最终结果

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int totalPrice = sc.nextInt(); //预算
        int targetNum = sc.nextInt();//需要的元器件种类
        int total = sc.nextInt(); //总共的元器件数量
        sc.nextLine();
        Device[] devices = new Device[total];

        for (int i = 0; i < total; i++) {
            String[] lines = sc.nextLine().split(" ");
            Device device = new Device(Integer.parseInt(lines[0]), Integer.parseInt(lines[2]), Integer.parseInt(lines[1]));
            devices[i] = device;
        }
        //先排序,根据种类、价格升序,可靠性降序排序
        //非必须,不排序也不影响
        Arrays.sort(devices, (o1, o2) -> {
            if (o1.getType() != o2.getType()) return o1.getType() - o2.getType();
            if (o1.getPrice() != o2.getPrice()) return o1.getPrice() - o2.getPrice();
            return o2.getReliability() - o1.getReliability();
        });
        System.out.println(largestReliability(devices, totalPrice, targetNum));
    }

    //从devices中选择targetNum种元器件,价格不超过totalPrice,能够得到的最高可靠性是多少?
    private static int largestReliability(Device[] device, int totalPrice, int targetNum) {
        //从total里面选取target
        LinkedList<Device> path = new LinkedList<>();//组合问题,临时选到path中去
        int[] used = new int[targetNum];
        dfs(device, 0, path, used, totalPrice, targetNum, Integer.MAX_VALUE);
        return ans;
    }

    private static void dfs(Device[] devices, int begin, LinkedList<Device> path, int[] used, int totalPrice, int targetNum, int reliability) {
        if (path.size() == targetNum) {
            if (totalPrice >= 0 && reliability > ans) {
                ans = reliability;
            }
            return;
        }
        for (int i = begin; i < devices.length; i++) {
            if (used[devices[i].getType()] == 1) continue;//剪枝,不能选择同类型的type
            path.addLast(devices[i]);
            used[devices[i].getType()] = 1;
            dfs(devices, i + 1, path, used, totalPrice - devices[i].getPrice(), targetNum, Math.min(reliability, devices[i].getReliability()));
            path.removeLast();
            used[devices[i].getType()] = 0;

        }
    }
}

class Device {
    private int type;
    private int price;
    private int reliability;

    public Device(int type, int price, int reliability) {
        this.type = type;
        this.price = price;
        this.reliability = reliability;
    }

    public int getType() {
        return type;
    }

    public int getPrice() {
        return price;
    }

    public int getReliability() {
        return reliability;
    }
}

推荐

如果你对本系列的其他题目感兴趣,可以参考华为OD机试真题及题解(JAVA),查看当前专栏更新的所有题目。

相关推荐
程序定小飞5 分钟前
基于springboot的健身房管理系统开发与设计
java·spring boot·后端
wxin_VXbishe15 分钟前
springboot在线课堂教学辅助系统-计算机毕业设计源码07741
java·c++·spring boot·python·spring·django·php
信仰_27399324336 分钟前
RedisCluster客户端路由智能缓存
java·spring·缓存
兰雪簪轩36 分钟前
仓颉语言内存布局优化技巧:从字节对齐到缓存友好的深度实践
java·spring·缓存
CaracalTiger1 小时前
本地部署 Stable Diffusion3.5!cpolar让远程访问很简单!
java·linux·运维·开发语言·python·微信·stable diffusion
okjohn1 小时前
《架构师修炼之路》——②对架构的基本认识
java·架构·系统架构·软件工程·团队开发
落笔映浮华丶1 小时前
蓝桥杯零基础到获奖-第4章 C++ 变量和常量
java·c++·蓝桥杯
合作小小程序员小小店2 小时前
web网页开发,在线%就业信息管理%系统,基于idea,html,layui,java,springboot,mysql。
java·前端·spring boot·后端·intellij-idea
陈果然DeepVersion2 小时前
Java大厂面试真题:从Spring Boot到AI微服务的三轮技术拷问(一)
java·spring boot·redis·微服务·kafka·面试题·oauth2
晨晖22 小时前
docker打包,启动java程序
java·docker·容器