【华为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),查看当前专栏更新的所有题目。

相关推荐
多多*1 分钟前
SpringBoot 启动流程六
java·开发语言·spring boot·后端·spring
极乐码农4 分钟前
Spring学习03-[Spring容器核心技术IOC学习进阶]
java·学习·spring
m0_588383324 分钟前
初学Spring之 JavaConfig 实现配置
java·spring
让你三行代码QAQ7 分钟前
SpringSecurity初始化过程
java·开发语言
Enaium36 分钟前
Rust入门实战 编写Minecraft启动器#1启动方法
java·后端·rust
滔滔不绝tao36 分钟前
SpringBoot拦截器
java·spring boot·spring
Mr_Richard38 分钟前
Java动态代理的实现方式
java·开发语言
sssjjww1 小时前
python输出日志out.log相关问题(缓存机制)
java·python·缓存
LL小蜗牛1 小时前
Java对象通用比对工具
java·开发语言
☆致夏☆2 小时前
Java-反射
java·开发语言